C# 如何使用 XmlDocument 向根元素添加多个命名空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/331568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:32:10  来源:igfitidea点击:

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

c#.netxmlnamespacesxmldocument

提问by Metro Smurf

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

我需要创建一个XmlDocument包含多个命名空间的根元素。正在使用 C# 2.0 或 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 XmlDocumentthat contains a root element with multiple namespaces?

我的问题:如何创建一个XmlDocument包含具有多个命名空间的根元素?

采纳答案by Oppositional

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.

但是,在您的示例/所需输出中,对两个不同的前缀使用相同的 XML 命名空间似乎很奇怪。希望这可以帮助。

回答by Dirk Vollmar

try to add the namespace attribute to the root element:

尝试将命名空间属性添加到根元素:

        XmlDocument doc = new XmlDocument();

        XmlElement root = doc.CreateElement("JOBS", "http://www.example.com");
        root.SetAttribute("xmlns:JOB", "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");
        root.AppendChild(docOutput);    

回答by Robert Rossney

You can explicitly create namespace prefix attributes on an element. Then when you add descendant elements that are created with both the same namespace and the same prefix, the XmlDocument will work out that it doesn't need to add a namespace declaration to the element.

您可以在元素上显式创建命名空间前缀属性。然后,当您添加使用相同命名空间和相同前缀创建的后代元素时,XmlDocument 将计算出它不需要向元素添加命名空间声明。

Run this example to see how this works:

运行此示例以查看其工作原理:

    using System;
    using System.Xml;

    static void Main(string[] args)
    {
        XmlDocument d = new XmlDocument();
        XmlElement e = d.CreateElement("elm");

        d.AppendChild(e);

        d.DocumentElement.SetAttribute("xmlns:a", "my_namespace");

        e = d.CreateElement("a", "bar", "my_namespace");
        d.DocumentElement.AppendChild(e);
        e = d.CreateElement("a", "baz", "other_namespace");
        d.DocumentElement.AppendChild(e);
        e = d.CreateElement("b", "bar", "my_namespace");
        d.DocumentElement.AppendChild(e);

        d.Save(Console.Out);

        Console.ReadLine();
    }