如何添加多个命名空间与XmlDocument的根元素? [英] How do I add multiple namespaces to the root element with XmlDocument?
本文介绍了如何添加多个命名空间与XmlDocument的根元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建一个的XmlDocument
包含多个命名空间根元素。我使用C#2.0或3.0
I need to create an XmlDocument
with a root element containing multiple namespaces. Am using C# 2.0 or 3.0
下面是我的code:
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("JOBS", "http://www.example.com");
doc.AppendChild(root);
XmlElement job = doc.CreateElement("JOB", "http://www.example.com");
root.AppendChild(job);
XmlElement docInputs = doc.CreateElement("JOB", "DOCINPUTS", "http://www.example.com");
job.AppendChild(docInputs);
XmlElement docInput = doc.CreateElement("JOB", "DOCINPUT", "http://www.example.com");
docInputs.AppendChild(docInput);
XmlElement docOutput = doc.CreateElement("JOB", "DOCOUTPUT", "http://www.example.com");
docOutputs.AppendChild(docOutput);
电流输出:
<JOBS xmlns="http://www.example.com">
<JOB>
<JOB:DOCINPUTS xmlns:JOB="http://www.example.com">
<JOB:DOCINPUT />
</JOB:DOCINPUTS>
<JOB:DOCOUTPUTS xmlns:JOB="http://www.example.com">
<JOB:DOCOUTPUT />
</JOB:DOCOUTPUTS>
</JOB>
</JOBS>
不过,我需要的输出是:
However, my desired output is:
<JOBS xmlns:JOBS="http://www.example.com" xmlns:JOB="http://www.example.com">
<JOB>
<JOB:DOCINPUTS>
<JOB:DOCINPUT />
</JOB:DOCINPUTS>
<JOB:DOCOUTPUTS>
<JOB:DOCOUTPUT />
</JOB:DOCOUTPUTS>
</JOB>
</JOBS>
我的问题:如何创建一个的XmlDocument
包含多个名称空间的根元素
My question: how do I create an XmlDocument
that contains a root element with multiple namespaces?
推荐答案
下面将生成您上面所要求的所需的输出:
The following will generate the desired output that you requested above:
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("JOBS");
root.SetAttribute("xmlns:JOBS", "http://www.example.com");
root.SetAttribute("xmlns:JOB", "http://www.example.com");
doc.AppendChild(root);
XmlElement job = doc.CreateElement("JOB");
XmlElement docInputs = doc.CreateElement("JOB", "DOCINPUTS", "http://www.example.com");
XmlElement docInput = doc.CreateElement("JOB", "DOCINPUT", "http://www.example.com");
docInputs.AppendChild(docInput);
job.AppendChild(docInputs);
XmlElement docOutputs = doc.CreateElement("JOB", "DOCOUTPUTS", "http://www.example.com");
XmlElement docOutput = doc.CreateElement("JOB", "DOCOUTPUT", "http://www.example.com");
docOutputs.AppendChild(docOutput);
job.AppendChild(docOutputs);
doc.DocumentElement.AppendChild(job);
不过,这似乎有些奇怪,在你的榜样,同样的XML命名空间是用来对付两个不同的prefixes /所需的输出。希望这有助于。
However, it seems odd that in your example/desired output that the same XML namespace was used against two different prefixes. Hope this helps.
这篇关于如何添加多个命名空间与XmlDocument的根元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文