Installing only one namespace in LINQ

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!

+4
source share
2 answers

This behavior is fundamental to the XML dataset. The xmlns declaration effect extends to all child content; since you did not specify a namespace for the child, the standard behavior is to put it in an unnamed namespace. To insert an element into an unnamed namespace inside the one that declares the default namespace, an xmlns='' declaration is required to reset the default namespace to unnamed.

To summarize, based on your edit:

  • If you don't need namespaces in child elements, then Explicit xmlns='' requires XML Specification Namespaces to override the namespace declared in the document element.
  • If you do not want the namespace names to be declared in your children (i.e. xmlns='' ), you need to make sure that the same namespace is added to the names of all of them in the code.

I do not know how to make XElement automatically inherit the namespace of its parent. However, after you finish creating the DOM, you can always process it to set up one namespace in everything, something similar to this:

 foreach(var element in doc.Descendants()) element.Name = aw + element.Name.LocalName; 

I do not think this is useful for anything other than a reset code, however, as this is likely to have adverse effects on maintainability. A number of XML documents that I had to solve with mix namespaces and “splitting” namespaces, like the code snippet above, will compress the semantics of the mixed namespace document.

+6
source

The only way I can think of is to compile the entire document into code, and then write a general purpose method to apply a single namespace to all elements. It will probably be awkward to get right, but certainly possible.

Basically, new XElement("Child") documented to create an element without a namespace - you cannot change it.

Another option is to extract all the names of your elements into variables (both local and static, read-only), and apply the namespace there:

 private static readonly XNamespace AdventureWorksNs = "http://www...."; private static readonly XName RootName = AdventureWorksNs + "Root"; private static readonly XName ChildName = AdventureWorksNs + "Child"; 

then

 XElement root = new XElement(RootName, new XElement(ChildName, "child content") ); 
0
source

All Articles