简体   繁体   中英

Creating an XmlElement without a namespace

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">

" 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM