Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #94 – Reading XML through LINQ with Silverlight

In Tip of the Day #93 I showed you how to read XML via XmlReader. However, a better and simpler approach (IMHO) would be to use LINQ.

Let’s start with the same XML file we used in Tip #93.

<?xml version="1.0" encoding="utf-8" ?>
<ImageTree>
  <Area name="grass">
    <Image name="Normal Grass">grass1.png</Image>
    <Image name="Dry Grass">grass2.png</Image>
    <Image name="Mixed Grass">grass3.png</Image>
    <Image name="Long Grass">grass4.png</Image>
  </Area>
  <Area name="tile">
    <Image name="Brick">brick.png</Image>
    <Image name="White Stone">stone.png</Image>
    <Image name="Cracked Stone">crackedstone.png</Image>
    <Image name="Black Brick">brick2.png</Image>
  </Area>
</ImageTree>

To use LINQ you will need to add a reference to System.Xml.Linq. To do this, right click on your References folder in your Silverlight application in the Solution Explorer and choose “Add Reference”. You will find this component on the .NET tab. In your source file add a using statement to reference System.Xml.Linq.

The function below will create a tree view out of the XML file that is shown above. A few important notes on this code:

  1. The XML file will need to be marked as Build Action=Content so that it gets placed into your XAP file. If you select this file, you can apply this setting in it’s properties in the property grid.
  2. XDocument.Load() is used to load the XML file from your XAP.
  3. The XDocument has a enumerable property called Descendants. You can specify what node you want to search for as the parameter of this property and go from there.
private void CreateTree()
{
    TreeViewItem areaItem = null;
    TreeView tv = new TreeView();
    TreeViewItem rootItem = new TreeViewItem();
    rootItem.Header = "Images";
    tv.Items.Add(rootItem);
 
    XDocument document = XDocument.Load("MapImages.xml");
 
    foreach (XElement element in document.Descendants("Area"))
    {
        areaItem = new TreeViewItem();
        areaItem.Header = element.FirstAttribute.Value;
        rootItem.Items.Add(areaItem);
 
        foreach (XElement imageElement in element.Descendants("Image"))
        {
            TreeViewItem imageItem = new TreeViewItem();
            imageItem.Tag = imageElement.Value;
            imageItem.Header = imageElement.FirstAttribute.Value;
            areaItem.Items.Add(imageItem);
        }
    }
 
    MainCanvas.Children.Add(tv);
}

As you can see from the code above it’s a lot simpler and straight forward than using XmlReader.

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

In Tip of the Day #93 I showed you how to read XML via XmlReader . However, a better and simpler approach

# February 11, 2009 3:03 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight Tips of the

# February 11, 2009 3:07 PM

Dew Drop - February 12, 2009 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - February 12, 2009 | Alvin Ashcraft's Morning Dew

# February 12, 2009 9:43 AM

Community Blogs said:

In This issue: Jim Lynn, Pencho Popadiyn, Tim Heuer, Scott Guthrie, Mike Snow, Maurice de Beijer, and

# February 12, 2009 6:39 PM

Programming with Silverlight, WPF & .NET » XML mit Silverlight lesen said:

Pingback from  Programming with Silverlight, WPF &amp; .NET &raquo; XML mit Silverlight lesen

# February 16, 2009 5:54 AM

Visual Web Developer Team Blog said:

Most Recent Posts: Silverlight Tip of the Day #106 - Setting Default Browser from within VS Silverlight

# April 6, 2009 6:13 PM

SharpGIS said:

What makes this LINQ? Shouldn't there be some LINQ queries in there? All I see is some manual node parsing, but no "language integrated queries".

# June 23, 2009 8:05 PM

NewsPeeps said:

Thank you for submitting this cool story - Trackback from NewsPeeps

# August 8, 2009 6:36 PM