How To Make XElement An XDocument

This is a cool trick that I discovered in Vb when working with XML. Vb has full support for XML in the language so much so that you can type in XML write in the code and IDE would do color highlighting to differentiate it from the code in the page. It would also do correct indenting of the code so XML is more readable. By default when you type in XML in Vb.net code, it is inferred as XElement. Below example shows this behavior.

image

In the above XML code, when I hover over the element that is assigned the XML content, the inferred type is returned as XElement. If you need to return XDocument that complies with all the XML specification, all you have to do is put some processing instructions on top of the XML element and visual studio would fix the type to be XDocument. Example below shows this behavior.

image

In the above screen shot, I have added XML processing instructors which causes the type inference to change to XDocument which we can confirm by hover over customers variable.

No Comments