I'm having problems with namespaces in LINQ. Therefore, by submitting an MSDN tutorial , we can do the following to add namespaces to an XML document with LINQ:
// Create an XML tree in a namespace. XNamespace aw = "http://www.adventure-works.com"; XElement root = new XElement(aw + "Root", new XElement(aw + "Child", "child content") ); Console.WriteLine(root);
What XML will give you:
<Root xmlns="http://www.adventure-works.com"> <Child>child content</Child> </Root>
But I want to do the following:
XNamespace aw = "http://www.adventure-works.com"; XElement root = new XElement(aw + "Roots"); XElement child = new XElement("Child", "child content"); root.Add(child); Console.WriteLine(root);
But this gives me the following xml:
<Root xmlns="http://www.adventure-works.com"> <Child xmlns="">child content</Child> </Root>
The problem is that I don't want xmlns="" in my children (EDIT: I don't need namespaces in my children) . I want it to look the same as the first method produces it. I am creating a rather large XML file, so I cannot add all the elements to the same declaration and should be able to use the Add and SetAttributeValue for several XElements still having xmlns on the first element. Any ideas how I can do this?
Thanks!
source share