【问题标题】:Setting XML namespaces with the System.Xml.Linq API使用 System.Xml.Linq API 设置 XML 命名空间
【发布时间】:2009-02-09 17:07:52
【问题描述】:

我在生成 XML 时遇到了问题:

<Root xmlns:brk="http://somewhere">
<child1>
    <brk:node1>123456</brk:node1>
    <brk:node2>500000000</brk:node2>
</child1>
</Root>

这段代码让我获得了大部分的帮助,但我无法获得节点前面的“brk”命名空间;

 var rootNode = new XElement("Root");
 rootNode.Add(new XAttribute(XNamespace.Xmlns + "brk", "http://somewhere"));

 var childNode = new XElement("child1");
 childNode.Add(new XElement("node1",123456));
 rootNode.Add(childNode);

我试过这个:

XNamespace brk = "http://somewhere";
childNode.Add(new XElement(brk+"node1",123456));

还有这个

XNamespace brk = "http://somewhere";
childNode.Add(new XElement("brk:node1",123456));

但两者都会导致异常。

【问题讨论】:

  • 你得到什么异常?使用 childNode.Add(new XElement(brk+"node1",123456)); 时,我没有得到任何异常和正确的结果;
  • System.Xml.XmlException: 前缀 '' 不能在同一个起始元素标记内从 '' 重新定义为 'somewhere'。

标签: c# xml linq-to-xml xnamespace


【解决方案1】:

您快到了,但您在第一个代码示例中犯了一个简单的错误。我相信这就是您所需要的:

XNamespace brk = "http://somewhere.com";
XElement root = new XElement("Root",
    new XAttribute(XNamespace.Xmlns + "brk", "http://somewhere.com"));

XElement childNode = new XElement("child1");
childNode.Add(new XElement(brk + "node1",123456));
root.Add(childNode);

这里的主要区别是我将node1 添加到childNode 如下:

childNode.Add(new XElement(brk + "node1",123456));

这个代码,给定一个XmlWriterXDocument 给我输出:

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:brk="http://somewhere.com">
  <child1>
    <brk:node1>123456</brk:node1>
  </child1>
</Root>

有关使用XNamespace 的详细信息,请参阅MSDN

【讨论】:

  • 好吧耶茨,让我们来个男人拥抱吧,你只是在后脚,因为你羡慕我的 var 用法。我不明白为什么你的应该工作而不是我的?你能告诉我吗?
  • 我很沮丧,但我们必须避免使用肮脏的 var - 我不想抓住任何东西。我认为这是可行的,因为在您的第一个代码示例中,您不执行“brk +“node1””,而只需执行“node1”。
  • 有趣的是,我尝试了您的第二个代码示例,其中包含 XNamespace 声明和 brk + "node1",但我没有遇到异常,所以我不确定为什么这对您不起作用.
  • 我不确定是不是这样!?我试过 brk+"node1"。这似乎取决于您是在根元素的构造函数还是在 Add 方法上设置命名空间。
【解决方案2】:

我认为问题在于根元素也需要有命名空间:

XElement root = new XElement("Root",
    new XAttribute(XNamespace.Xmlns + "brk", "http://somewhere.com"));

需要:

XElement root = new XElement(brk + "Root",
    new XAttribute(XNamespace.Xmlns + "brk", "http://somewhere.com"));

【讨论】:

    【解决方案3】:

    这是独奏,工作正常。

     using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        using System.Xml;
        using System.Xml.Linq;
        using System.Xml.XPath;
        using System.Xml.Serialization;
    
        namespace CreateSampleXML
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();            
                    XNamespace xm = "http://somewhere.com";
                    XElement rt= new XElement("Root", new XAttribute(XNamespace.Xmlns + "brk", "http://somewhere.com"));
                    XElement cNode = new XElement("child1");
                    cNode.Add(new XElement(xm + "node1", 123456));
                    cNode.Add(new XElement(xm + "node2", 500000000));
                    rt.Add(cNode);
                    XDocument doc2 = new XDocument(rt);
                    doc2.Save(@"C:\sample3.xml");
                }
            }       
        }
    

    【讨论】:

    • 请在您的答案中讨论您的代码。不要只提供代码。
    • 这就是解决方案。 @EvertonAgner
    猜你喜欢
    • 1970-01-01
    • 2015-01-09
    • 2019-02-19
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多