Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Deep XML Support for VB 9.0

Deep XML Support for VB 9.0

Deep XML Support for VB 9.0

Microsoft's XML team demonstrates some of the new features for VB 9 including XLINQ and XML Literals by converting iTunes Playlists into Zune Playlists.

The first exercise demonstrates how easy it is to query XML data. Once the XML file is loaded into memory, a single query expression is all that is needed to extract the names of all the play lists.

Dim itunesLib As XElement = XElement.Load(itunesLibLocation)
Dim itunesPlaylists = From key In itunesLib... _
                      Where key.Value = "Playlist ID" _
                      Select key.ElementsBeforeSelf("string").Value

Avner writes,

This is a great example of how seamless is the LINQ to Xml integration in VB 9.0, in the "From" clause I use the VB xml descendants axis to get all the "key" elements, for each element I use the "Value" property on XElement to filter only the playlist ID keys. Finally I use the "Value" extension property in VB 9.0 to select the value of the first "string" element before that "key" element. Moving between XLinq API and VB 9.0 Xml properties is very natural and intuitive.

For those of you who are unfamiliar with the new syntax, the descendants axis operator (...) is used to match elements no matter how deep they are in the tree.

Avner continues the demonstration by showing how XML Literals work. XML Literals are a new feature that only VB will be getting. They allow straight XML to be embedded in source code much in the same way assembly is embedded in C. In order to embed expressions calculated at runtime, it uses the classic ASP syntax (<%= %>). Both scalar values and XML sub-trees may be included this way.

Avner concludes with a syntax design discussion about what computed variables should look like in VB/LINQ. Options include using "Let" in front of each computed value clause like C#, using "From" instead of Let, or skipping the whole debate by just including it in the first From clause.

Rate this Article