March 1, 2021
Hot Topics:


  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Querying XML by Using LINQ to XML

In this example, you'll load an example XML data file that contains information on real estate listings. In this case, I have an XML file from what used to be expo.live.com that will be used. I've abbreviated the sample file to a single entry so that it fits into the article text and saved it in a file called exporss.xml.

<?xml version="1.0" encoding="utf-8"?><rss version="2.0"     xmlns:classifieds="http://expo.live.com/ns/2006/1.0"     xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">   <channel>      <title>Expo RSS Feed</title>      <link>http://expo.live.com/</link>      <description>Expo RSS feed</description>      <language>en-US</language>      <pubDate>Wed, 25 Jul 2007 15:46:25 GMT</pubDate>      <classifieds:totalListings>2384</classifieds:totalListings>      <item>         <title>            Home for Rent early August: 4 bedrooms—2.5 baths         </title>         <link>            http://expo.live.com/ViewListing.aspx?lid=5870094         </link>         <description>            Light and bright home that says "Welcome Home".         </description>         <pubDate>Wed, 25 Jul 2007 05:20:00 GMT</pubDate>         <classifieds:listingid>5870094</classifieds:listingid>         <classifieds:expirationDate>            Wed, 19 Sep 2007 05:15:00 GMT         </classifieds:expirationDate>         <classifieds:currency>USD</classifieds:currency>         <classifieds:price>2200.00</classifieds:price>         <classifieds:category classifieds:id="18"            classifieds:name="Houses"            classifieds:transactionType="For Rent">         </classifieds:category>         <classifieds:postedBy>Joannede</classifieds:postedBy>         <classifieds:location>            <classifieds:address></classifieds:address>            <classifieds:city>Redmond</classifieds:city>            <classifieds:state>WA</classifieds:state>            <classifieds:country>US</classifieds:country>            <classifieds:postcode>98052</classifieds:postcode>            <classifieds:latitude>               47.6870625585192             </classifieds:latitude>            <classifieds:longitude>               -122.118400170762         </classifieds:longitude>         </classifieds:location>         <geo:lat>47.6870625585192</geo:lat>         <geo:long>-122.118400170762</geo:long>         <classifieds:details>            <classifieds:NUMBER_BEDROOMS>               4            </classifieds:NUMBER_BEDROOMS>            <classifieds:NUMBER_BATHROOMS>               2.5             </classifieds:NUMBER_BATHROOMS>            <classifieds:YEAR_BUILT></classifieds:YEAR_BUILT>            <classifieds:LOT_SIZE>2500</classifieds:LOT_SIZE>         </classifieds:details>      </item>   </channel></rss>

The following sample code demonstrates a static method that will load up a copy of the sample XML, query it for houses in the given range, and then return a string based on what is found.

static string QueryXML(int lowPrice, int hiPrice){   XNamespace classifieds = "http://expo.live.com/ns/2006/1.0";   XNamespace geo = "http://www.w3.org/2003/01/geo/wgs84_pos#";   XElement expoData = XElement.Load(@"exporss.xml");   var houses = from item in      expoData.Elements("channel").Elements("item")      let price = (double)item.Element(classifieds + "price")      where price > lowPrice && price < hiPrice      orderby price      select item;   string strOut = "";   foreach (var house in houses)   {      strOut += String.Format("\r\nPrice = {0} {1}",         (string)house.Element(classifieds + "price"),         (string)house.Element("description"));   }   return strOut;}


You now have seen LINQ to XML and the simplicity that it can bring to working with XML. Additionally, you saw a mechanism for querying with LINQ to XML that has been shown in benchmark studies from Microsoft to outperform DOM. Hopefully, it also provided you with additional appreciation for the unified access that LINQ can provide to various data sources.

Future Columns

The topic of the next column is likely to be LINQ to SQL. If you have something else in particular that you would like to see explained here, you could reach me at mark.strawmyer@crowehorwath.com.

About the Author

Mark Strawmyer is a Senior Architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Horwath LLP in Indianapolis, Indiana. He specializes in the architecture, design, and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the fifth year in a row. You can reach Mark at mark.strawmyer@crowehorwath.com.

Page 2 of 2

This article was originally published on November 17, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date