I have an XML file that I need to apply a namespace to at runtime. I’ve searched the net and most examples seem to suggest using the “SetAttributeValue” function. When I use the code below it throws an exception when I try to “ToString()” with the following error:
The prefix '' cannot be redefined from '' to 'http://schemas.datacontract.org/2004/07/' within the same start element tag.
Any ideas what is going wrong?
XDocument data = XDocument.Parse("<Root><Stuff>Test</Stuff></Root>");
string ns = "http://schemas.datacontract.org/2004/07/";
data.Root.SetAttributeValue("xmlns",ns);
SaveFile(data.ToString());