Xml Axis properties, VB.NET's second cool Xml language construct

Yesterday I posted about the Xml literal support within Visual Basic 9.0 and how it has me envious of VB.NET programmers.  While Xml literals are the most notable language feature available in VB (and not C#) there is one other language construct that is also unique to Visual Basic, Xml axis properties.

The XLinq API provides several core extension methods that developers will use when querying Xml using Linq.  The most notable extension methods are Elements and Attributes.  Elements returns an IEnumerable<XElement> of the child elements within an Xml fragment.  Elements provides an overload that accepts a XName which allows the child elements returned to be limited to those of the relevant XName.  I should note that in most scenarios developers will use a simple string such as “customer” which will be implicitly converted into a XName.  The Attributes method provides the same behavior as Elements except instead of returning an IEnumerable<XElement> it returns an IEnumerable<XAttribute>.

As an example lets pretend we want to write a method that will return all the items within a trimmed down version of Scott Gu’s RSS feed. 

    1 Public Function GetItems() As IEnumerable(Of XElement)

    2     Dim rss As XElement = <rss version="2.0">

    3                             <channel>

    4                               <title>Scott Gu's Blog</title>

    5                               <link>http://weblogs.asp.net/scottgu/</link>

    6                               <description>Scott Guthrie's Blog on ASP.NET and .NET</description>

    7                               <item>

    8                                 <title>Recipe: Dynamic Site Layout and Style Personalization with ASP.NET</title>

    9                                 <link>http://weblogs.asp.net/scottgu/arc...Personalization-with-ASP.NET--.aspx</link>

   10                                 <pubDate>Sun, 23 Jul 2006 02:19:00 GMT</pubDate>

   11                               </item>

   12                               <item>

   13                                 <title>Upcoming Free ASP.NET Team Webcasts -- Live from Redmond</title>

   14                                 <link>http://weblogs.asp.net/scottgu/arc..._2200_Live-from-Redmond_2200_.aspx</link>

   15                                 <pubDate>Sat, 22 Jul 2006 02:15:00 GMT</pubDate>

   16                               </item>

   17                             </channel>

   18                           </rss>

   19 

   20     Return rss.Element("channel").Elements("item")

   21   End Function

After using the Xml literal support within VB 9 to create a trimmed down version of Scott’s feed we use the Element and Elements extension methods to select the data that we’re interested in.  As you can see (line 20) I use the Element() method to select the channel element within the feed and then select all items within the channel element by calling Elements() with a XName of “item”.  As an alternative VB 9 provides axis properties which allow the above to be expressed using a more compact syntax.

   Return rss...<item>

This expressions uses the descendants axis support in VB 9 which can be interpreted as: give me all “item” elements underneath the rss element regardless of where they occur in the heirarchy.  VB9 also has support for child axis’ which can be used to get all elements of a given name directly under a given element.  For example to return all the channel elements underneath the root rss element one could use the following syntax.

   Return rss.<channel>

If your not interested in elements but instead want to get at attributes you can use the attribute axis syntax:

   Return rss.@version 

And finally if you want to grab a particular item you can use the extension indexer to select the item of interest off the resulting sequence like so:

   Return rss...<item>(3)

While Xml literals and axis properties aren’t something that is likely to make or break a programmers language decision it does make working with Xml in VB 9 a “pure joy” ® .  For more information checkout the Xml team’s VB 9.0 Xml Features post and Erik’s Gilad is Right post on lambda the ultimate.

Post a Comment

 
 
Prove you're not a spammer: 
8 + 7 =