【问题标题】:Using Linq to XML to write NAnt .build files使用 Linq to XML 编写 NAnt .build 文件
【发布时间】:2010-02-11 15:45:49
【问题描述】:

我正在尝试使用 C# 和 Linq to XML 创建一个 XML 文件,但是遇到了 nant 命名空间的问题。

var myXElement = new XElement("project", new XAttribute("name", "MySystemName"), new XAttribute("default", "myNAntTargetName"), new XAttribute("xmlns", "http://nant.sf.net/schemas/nant.xsd"));

myXElement.Save("c:\foo.xml");

因为我试图强制进入命名空间 XAtrribute(到http://nant.sf.net/schemas/nant.xsd) .Save 失败,说:

未处理的 XmlException:前缀 '' 不能在同一起始元素标记内从 '' 重新定义为 'http://nant.sf.net/schemas/nant.xsd'。

如果我只是删除命名空间属性,文件保存得很好,但我需要命名空间,否则我的 NAnt 命令会失败。

有什么建议吗?

【问题讨论】:

    标签: xml linq linq-to-xml nant xml-namespaces


    【解决方案1】:

    在网上找了很久才找到这个:

    http://guyellisrocks.com/coding/the-prefix-cannot-be-redefined-from-within-the-same-start-element-tag/

    基本上,我需要定义一个命名空间并将其应用到代码中(无处不在),当保存文件时,命名空间被省略。

    希望这对其他人有帮助!如果是,请给我投票?

    【讨论】:

      猜你喜欢
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多