XML Parsing using LINQ

XML file that I used is

<?xml version="1.0" encoding="utf-8" ?>
<Tutorials>
  <Tutorial>
    <Author>The Reddest</Author>
    <Title>
      Creating an XP Style WPF Button with Silverlight
    </Title>
    <Date>2/20/2008</Date>
  </Tutorial>
  <Tutorial>
    <Author>The Fattest</Author>
    <Title>
      Flex And Yahoo Maps
    </Title>
    <Date>2/12/2007</Date>
  </Tutorial>
  <Tutorial>
    <Author>The Tallest</Author>
    <Title>
      WPF Tutorial - Creating A Custom Panel Control
    </Title>
    <Date>2/18/2008</Date>
  </Tutorial>
</Tutorials>

 

First, we’re going to use some simple LINQ syntax to create some anonymous objects with the properties, Author, Title, and Date.

XDocument xmlDoc = XDocument.Load("TestFile.xml");

var tutorials = from tutorial in xmlDoc.Descendants("Tutorial")
                select new
                {
                  Author = tutorial.Element("Author").Value,
                  Title = tutorial.Element("Title").Value,
                  Date = tutorial.Element("Date").Value,
                };

 

The result of this code will be an IEnumerable collection filled with anonymous objects. What’s nice about Visual Studio is that you’ll even get dot completion for anonymous types.

Anonymous objects are well and good, but usually I want something a little better defined to pass around my application. Let’s look at how to create a List of Tutorialobjects.

public class Tutorial
{
  public string Author { get; set; }
  public string Title { get; set; }
  public DateTime Date { get; set; }
}

...

List<Tutorial> tutorials =
  (from tutorial in xmlDoc.Descendants("Tutorial")
   select new Tutorial
   {
     Author = tutorial.Element("Author").Value,
     Title = tutorial.Element("Title").Value,
     Date = DateTime.Parse(tutorial.Element("Date").Value),
   }).ToList<Tutorial>();

At this point, you’re probably floored at how quickly I was just able to parse that XML document into my collection – I know I am. Let’s look at one simple piece I haven’t mentioned yet, the where clause. Just like SQL, LINQ has the ability to filter results using the where keyword. Let’s filter down my collection to only tutorials written by The Tallest (his are always the best anyway).

List<Tutorial> tutorials =
  (from tutorial in xmlDoc.Descendants("Tutorial")
   where tutorial.Element("Author").Value == "The Tallest"
   select new Tutorial
   {
     Author = tutorial.Element("Author").Value,
     Title = tutorial.Element("Title").Value,
     Date = DateTime.Parse(tutorial.Element("Date").Value),
   }).ToList<Tutorial>();

Now my list of tutorials will only contain those written by The Tallest.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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