VB 9.0 Xml: Improvements to global Xml namespaces
Global Xml namespaces in VB 9.0 provide the ability to create alias for Xml namespace that can be used in the Xml axis properties and in the Xml literals. The following is an example from my previous post:
Imports ns = "https://www.w3.org/1999"
Module Module1
Sub Main()
Dim book = _
<ns:Book>
<ns:Title>Learning Programming Using VB</ns:Title>
</ns:Book>
Console.WriteLine(book.<ns:Title>.Value)
End Sub
End Module
New syntax
In the CTP version we decided to choose a very similar syntax to the CLR imports syntax where the main difference is that Xml namespace is a quoted string. We chose that syntax because in both cases the "Imports" declaration provides a convenient way to create fully qualified names. However the feedback we got from both internal and external users, indicated that the syntax should make the distinction between importing CLR and Xml namespaces clearer since they are used in different contexts in the program. The new syntax we chose is the Xml syntax for namespaces, the following is an example of the new syntax applied on the "Imports" statement above:
Imports xmlns:ns="https://www.w3.org/1999"
This syntax and coloring make it very clear that this is not CLR namespace, it also allow copy of namespaces declarations from Xml documents to the "Imports" section in the VB program.
Implicit reification of Xml namespaces
There are many cases where it is useful to use the global Xml namespace declaration in other parts of the program, for example when using XLinq API directly. In VB 9.0 the global Xml namespaces will be implicitly reified to a variable with the alias name.
Consider the following example of creating XName using the Imports declaration above:
Dim name As XName = ns + "Root"
Console.WriteLine(name)
Result:
{https://www.w3.org/1999}Root
Note that VB scoping rules apply to global Xml namespaces aliases in the same way they apply to global variables and to CLR namespaces aliases.
That's it, hope you like these improvements, and please let us know what you think.
Avner Aharoni
Comments
- Anonymous
June 25, 2006
Much better XML Team. - Anonymous
June 25, 2006
It's great for what it is in particular and in genral - somebody still care about VB.NET developers.
I hope that current discrimination of VB.NET inside LINQ project will go away. - Anonymous
July 03, 2006
I think that's a nice improvement. Integrated into VB but still retaining it's XML-ness. - Anonymous
July 19, 2006
Microsoft XML Team's WebLog : VB 9.0 Xml...