Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

c# - How do I add multiple namespaces to the root element with XmlDocument?

I need to create an XmlDocument with a root element containing multiple namespaces. Am using C# 2.0 or 3.0

Here is my 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);  

The current output:

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

My question: how do I create an XmlDocument that contains a root element with multiple namespaces?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

However, it seems odd that in your example/desired output that the same XML namespace was used against two different prefixes. Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...