BT

Deep XML Support for VB 9.0

| by Jonathan Allen Follow 530 Followers on Feb 07, 2007. Estimated reading time: 1 minute |

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

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT