0

I am using the CreateElement() method of my XmlDocument like this:

XmlElement jobElement = dom.CreateElement("job");

But what I get in the outcome is a job element with an empty namespace.

<job xmlns="">xyz</job>

And the program that is supposed to read this Xml will no longer detect this job element. I thought this question gives an answer, but it does not. How can I create a pure job element?

<job>xyz</job>

Update: This is the root element of my document:

<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
Community
  • 1
  • 1
disasterkid
  • 6,948
  • 25
  • 94
  • 179
  • Every node has a namespace - it's conceptually not optional. But if the namespace is not explicitly specified on a node, it inherits the namespace of its parent. – Timothy Shields Oct 02 '14 at 15:44
  • @TimothyShields but it's parent should not have a namespace either. – disasterkid Oct 02 '14 at 15:46
  • Then the parent will inherit from its parent. Ultimately, at the very least, the root node of your document should have an explicit namespace that all other descendants in the document tree inherit (assuming you are using > XML version 1.0). – Timothy Shields Oct 02 '14 at 15:47
  • @TimothyShields updated my question with the root element of my document, how can I get rid of the xmlns part now? – disasterkid Oct 02 '14 at 16:32
  • 1
    Great, now all you have to do is set the XML namespace of each of the descendant nodes to be `http://quartznet.sourceforge.net/JobSchedulingData`. When the XML is rendered to text, it will not write out redundant `xmlns` attributes. So you'll get something that looks like this: `xyz` – Timothy Shields Oct 02 '14 at 17:10

1 Answers1

4

"I thought this question gives an answer, but it does not."

Actually it does. Set <job> element to be in the default namespace (unprefixed namespace that, in this case, was declared at the root level) :

XmlElement jobElement = dom.CreateElement("job", "http://quartznet.sourceforge.net/JobSchedulingData");
jobElement.InnerText = "xyz";

This way, in the XML markup, <job> element simply inherits it's ancestor's default namespace (no local-empty default namespace will be created) :

<job>xyz</job>
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137