If a node belongs to a namespace, it’s children by default belong to the same namespace. So there’s no need to provide an xmlns attribute on each child, which is good.
However.
If I create two nodes like this:
Dim parent = <parent xmlns="http://my.namespace.org"/>
Dim child = <child xmlns="http://my.namespace.org">value</child>
parent.Add(child)
Console.WriteLine(parent.ToString)
The result is this:
<parent xmlns="http://my.namespace.org">
<child xmlns="http://my.namespace.org">value</child>
</parent>
But, if create them in a less convenient way:
Dim parent = <parent xmlns="http://my.namespace.org"/>
Dim child As New XElement(XName.Get("child", "http://my.namespace.org")) With {.Value = "value"}
parent.Add(child)
Console.WriteLine(parent.ToString)
The result is more desirable:
<parent xmlns="http://my.namespace.org">
<child>value</child>
</parent>
Obviously, I’d prefer to use the first way because it is so much more intuitive and easy to code. There’s also another reason to not use method 2 — sometimes I need to create nodes with XElement.Parse, parsing a string that contains an xmlns attribute, which produces exactly same results as method 1.
So the question is — how do I get the pretty output of method 2, creating nodes as in method 1? The only option I see is to create a method that would clone given XElement, effectively recreating it according to method 2 pattern, but that seems ugly. I’m looking for a more obvious solution I overlooked for some reason.
Ugh…namespaces – they will be the death of me.
Here you go:
To be able to use
XElement.Parseand keep child nodes in sync with their parent nodes’ namespaces, it’s best to use global namespaces. Really easy to do in VB.NET. At the top of your module/class, just use an Imports and all parents and children will use this namespace. For example:Note that the
<child/>element is created first. The same would apply to a non-default namespace, likeImports <xmlns:p="http://parent.namespace.org">and then creating with<p:child/>and<p:parent/>.I once read, but have yet to find again, that mixing XML Literals with I-don’t-know-what-you-call-it-but-it’s-that-
parent.Add(something)-thing is a bad idea.