使用Linq to XML编写NAnt .build文件

时间:2010-02-11 15:45:49

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

我正在尝试使用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'。

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

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

在搜索了很长时间后在互联网上找到了这个:

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

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

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