LINQ2XMl and namespaces
In Xml namespace are used extensively to distinguish between tag names, you can use namespaces directly with XElement class thanks to the XNamespace class, here is an example.
|
|
This code works but the output is quite clumsy.
|
|
The namespace does not have an alias, so it is used as the default namespace, thus, the element AnotherElement, that has no namespace has the attribute xmlns=”” to reset it to the default namespace. This is really bad, so each time that you use XNamespace, does not forget to set an alias in the root node.
|
|
This code is similar to the old one, but with the fundamental addition of a new XAttribute object (line 2), that creates the alias for the namespace, now the XML output of the fragment is.
|
|
The fragment is really cleaner, now all the node that are in the namespace are prefixed with n. But this is not enough, suppose you have to compose various fragment of XML contained in XElement with namespaces, this is a typical snippet.
|
|
I create an element2 with a namespace and an alias, then I create another element with the same namespace, finally I add an element to the other, the result is this XML fragment.
|
|
The result is not so good, because the inner node still have the attribute for the namespace alias declaration. Since I know that all namespace alias are contained in the root node, I can remove all subsequent namespace alias declaration.
|
|
The code is really straightforward, with Descendants() I take all the nodes except the root, than I look for all inner declaration of the alias, and removed each instance with Remove(), the result is
|
|
This is cleaner than the previous one, because it declare namespace alias only in the root node of the XML fragment.
alk.