views:

101

answers:

1

I'm trying to create an XML file using C# and Linq to XML, but am having problems with the nant namespace.

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

Because I'm trying to force-in the namespace XAtrribute (to http://nant.sf.net/schemas/nant.xsd) the .Save fails, saying:

XmlException Unhandled: The prefix '' cannot be redefined from '' to 'http://nant.sf.net/schemas/nant.xsd' within the same start element tag.

If I simply remove the namespace attribute, the file saves fine, but I need the namespace in there, as otherwise my NAnt commands fail.

Any suggestions?

A: 

Found this on the internet after searching for a long time:

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

Basically, I needed to define a namespace and apply it in code (everywhere) and when the file is saved, the namespaces are omitted.

Hope this helps someone else! If so, up-vote me?

Brett Rigby