简体   繁体   English

创建不带名称空间的XmlElement

[英]Creating an XmlElement without a namespace

I am using the CreateElement() method of my XmlDocument like this: 我正在使用XmlDocumentCreateElement()方法,如下所示:

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

But what I get in the outcome is a job element with an empty namespace. 但是我得到的结果是一个带有空名称空间的job元素。

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

And the program that is supposed to read this Xml will no longer detect this job element. 并且本应读取此Xml的程序将不再检测到此job元素。 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) : <job>元素设置为默认名称空间 (在本例中是在根级别声明的无前缀名称空间):

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) : 这样,在XML标记中, <job>元素仅继承其祖先的默认名称空间(不会创建本地为空的默认名称空间):

<job>xyz</job>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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