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 Scotts feed we use the Element and Elements extension methods to select the data that were 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)
From novice to tech pro — start learning today.