Using LINQ to XML (and how to build a custom RSS Feed Reader with it)


What is LINQ to XML?

LINQ to XML is a built-in LINQ data provider that is implemented within the “System.Xml.Linq” namespace in .NET 3.5.

LINQ to XML provides a clean programming model that enables you to read, construct and write XML data.  You can use LINQ to XML to perform LINQ queries over XML that you retrieve from the file-system, from a remote HTTP URL or web-service, or from any in-memory XML content. 

LINQ to XML provides much richer (and easier) querying and data shaping support than the low-level XmlReader/XmlWriter API in .NET today.  It also ends up being much more efficient (and uses much less memory) than the DOM API that XmlDocument provides. 

Using LINQ to XML to query a local XML File

To get a sense of how LINQ to XML works, we can create a simple XML file on our local file-system like below that uses a custom schema we’ve defined to store RSS feeds:

I could then use the new XDocument class within the System.Xml.Linq namespace to open and query the XML document above.  Specifically, I want to filter the elements in the XML file and return a sequence of the non-disabled RSS feeds (where a disabled feed is a element with a “status” attribute whose value is “disabled”).  I could accomplish this by writing the code below:

Notice in the code-snippets above how I’m loading the XML file using the XDocument.Load(path) static method – which returns back an XDocument object.  Because I’m running this code within ASP.NET, I’m using the Server.MapPath(path) helper method to resolve the correct path for my XML file relative to the page I’m running the code on.

Once I have an XDocument object for my XML file I can then write a LINQ query expression to retrieve the XML data I’m looking for.  In the code above I’m querying over each of the elements within the XML file.  This is driven by this opening clause in the LINQ query expression:

from feed in feedXML.Decedents(“Feed”)

I’m then applying a filter that only returns back those “Feed” elements that either don’t have a “status” attribute, or whose “status” attribute value is not set to “disabled”:

Where (feed.Attribute(“status”) Is Nothing) OrElse (feed.Attribute(“status”).Value <> “disabled”)

I am then using the select clause in our LINQ expression to indicate what data I want returned.  If I simply wrote “select feed”, LINQ to XML would return back a sequence of XElement objects that represents each of the XML element nodes that match my filter.  In the code samples above, though, I am using the shaping/projection features of LINQ to instead define a new anonymous type on the fly, and I am defining two properties on it – Name and Feed – that I want populated using the and sub-elements under each element:

Select Name = feed.Element(“Name”).Value, Url = feed.Element(“Url”).Value

As you can see above (and below), I can then work against this returned sequence of data just like I would any collection or array in .NET.  VS 2008 provides full intellisense and compilation checking support over this anonymous type sequence:

I can also data-bind the results against any UI control in ASP.NET, Windows Forms, or WPF.  For example, assuming I had a dropdownlist control defined in my page like so:

I could use the below LINQ to XML code to databind the results to it:

This will then produce a nice drop-downlist in our HTML page like so:

More from author…

  • Using LINQ to XML to Retrieve a Remote RSS XML Feed
  • Using LINQ Sub-Queries within a LINQ to XML Query Expression
  • Putting it all Together with a Simple RSS Feed Reader

Using LINQ to XML (and how to build a custom RSS Feed Reader with it) Tags:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s