LINQ Cookbook, Recipe 8: Querying XML Using LINQ (Doug Rothaus)

LINQ Cookbook, Recipe 8: Querying XML Using LINQ (Doug Rothaus)

  • Comments 6


·         Visual Studio 2008 (Beta2 or Higher)


Categories: LINQ-To-XML



·         Create a new Console Application in Visual Basic.

·         Create an RSS document. Add the following code to Sub Main.

    Dim rss = <?xml version="1.0"?>

              <rss version="2.0">


                  <title>LINQ to XML in Visual Basic</title>

                  <description>Sample RSS Feed</description>


                  <pubDate>Tue, 18 Sep 2007 00:56:12 GMT</pubDate>


                    <title>Basic Instincts: Lambda Expressions</title>

                    <description>A new feature added to Visual Basic 9 to

                                 support Language Integrated Queries (LINQ),

                                 which adds data programmability to

                                 Visual Basic.</description>

                    <pubDate>Thu, 27 Sep 2007 23:54:55 GMT</pubDate>



                    <title>Visual Basic Pack for Visual Studio 2005 SDK</title>

                    <description>The Visual Basic Pack for the Visual Studio

                                 2005 SDK includes SDK samples converted into

                                 the Visual Basic language and a new wizard for

                                 generating Visual Basic-based integration

                                 packages for Visual Studio.</description>

                    <pubDate>Mon, 17 Sep 2007 23:58:49 GMT</pubDate>



                    <title>XML to Schema Tool</title>

                    <description>The XML to Schema tool is a free project item

                                 template to automate creation of XML schema

                                 sets from any number of XML documents. If you

                                 are working with Language Integrated Queries

                                 (LINQ) to XML in Visual Basic 9, this utility

                                 can significantly improve your editing

                                 experience by adding XML schemas (.xsd files)

                                 to your project that add IntelliSense for XML


                    <pubDate>Mon, 17 Sep 2007 23:59:43 GMT</pubDate>



                    <title>Line and Shape Controls in the Power Packs</title>

                    <description>Download the latest version of the Visual

                                 Basic 2005 Power Packs which now includes Line

                                 and Shape controls that enable you to draw

                                 lines, ovals, and rectangles on forms and

                                 containers at design time making it much easier

                                 to enhance the look of your user


                    <pubDate>Mon, 20 Aug 2007 20:24:25 GMT</pubDate>





You use the same LINQ syntax to query XML in Visual Basic as you would to query a collection, data from SQL Server, and so on. The difference is in how you reference the XML elements and attributes. The XML features in Visual Basic enable you to reference XML elements and attributes as first class data objects. That is, you can use XML syntax in your object-oriented Visual Basic code.

For example, to reference the <channel> element from the XDocument object created earlier in this topic, you would simply code: rss.<rss>.<channel>. While this may seem to reference only one <channel> element, XML in Visual Basic actually returns a collection of all of the <channel> elements that are sub-elements of the <rss> object. You can then refer to particular items in the collection by index, or, as we’ll see in this topic, you can perform a LINQ query over that collection of elements. To refer to any descendant of an XML element, use the descendant syntax, “...” as in rss.<rss>...<title>. To refer to an attribute, use the attribute syntax, “@”, as in rss.<rss>.@version. These options for referring to elements and attributes in an XDocument or XElement object are called XML Axis Properties.

Note: The XML features in Visual Basic include XML IntelliSense, which provides selection lists for possible XML attributes and child elements when you are coding using LINQ to XML objects such as XDocument or XElement object. To enable XML IntelliSense in Visual Basic, you simply need to include an XML Schema Definition (XSD) file in your project. The XML tools in Visual Basic will even create an XSD file for you, based on the contents of an XML file, if you don’t have an XSD file. For more information, see XML IntelliSense in Visual Basic.

Using the XML Axis Properties available in Visual Basic, we can query the XML content using LINQ. For example, the following query performs a free text search of the content from the <title> and <description> elements of an <item> in an RSS feed. Add the code to Sub Main after the code that creates the XDocument object.

    Dim itemList1 = From item In rss.<rss>.<channel>.<item> _

                    Where item.<description>.Value.Contains("LINQ") Or _



    Console.WriteLine("Items containing 'LINQ'" & vbCrLf)

    For Each item In itemList1

      Console.WriteLine(vbTab & "Title: " & item.<title>.Value)

      Console.WriteLine(vbTab & "Description: " & item.<description>.Value)




Note that the code example searches the Value property of an XML element to find a match. The Value property returns the contents of the XML element as a string. Without it, the XML Axis Property would return a collection of elements based on the element name. When searching XML attributes, you don’t need to specify the Value property. The XML Axis property will return the value of the first matching attribute, by default.

Because the Value property always returns a string, you may need to cast values as a particular type to perform your query. For example, the following code example searches for the items published in the last 20 days. Add the code to the end of Sub Main.

    Dim itemList2 = From item In rss.<rss>.<channel>.<item> _

                    Let pubDate = DateTime.Parse(item.<pubDate>.Value) _

                    Where pubDate >= _

                          DateTime.Now.Subtract(New TimeSpan(20, 0, 0, 0)) _

                    Select item


    Console.WriteLine("Items published in the last 20 days" & vbCrLf)

    For Each item In itemList2

      Console.WriteLine(vbTab & "Title: " & item.<title>.Value)

      Console.WriteLine(vbTab & "Publish Date: " & item.<pubDate>.Value)




Press F5 to see the code run.

Leave a Comment
  • Please add 7 and 4 and type the answer here:
  • Post
Page 1 of 1 (6 items)