Posted by: Zeeshan Amjad | March 22, 2011

Using LINQ to display XML data in DataGrid revisited


We already saw one example of loading XML data into grid using LINQ here. But in that example our XML file (or data to be exact) contains only element. We didn’t do any operation with attributes. In XML we can define the same data with the help of attributes. Let’s take a look at our XML file first. Here is our new XML file, this file contains the same data but using attributes rather than element.

Code Snippet
    <portfolio>
    <stock shares="100" symbol="MSFT" price="$70.00">
        <info companyname="Microsoft Corporation" website="http://www.microsoft.com&quot; />
    </stock>
    <stock shares="100" symbol="AAPL" price="$107.00">
        <info companyname="Apple Computer, Inc." website="http://www.apple.com&quot; />
    </stock>
    <stock shares="100" symbol="DELL" price="$50.00">
        <info companyname="Dell Corporation" website="http://www.dell.com&quot; />
    </stock>
    <stock shares="100" symbol="INTC" price="$115.00">
        <info companyname="Intel Corporation" website="http://www.intel.com&quot; />
    </stock>
</portfolio>

 

Our program is almost similar to our previous program, the only difference is now we are using Attribute method of XElement class. Here is our modified code.

Code Snippet
var result = (from c in doc.Descendants("stock")
        select new
        {
            Shares = c.Attribute("shares").Value,
            Symbol = c.Attribute("symbol").Value,
            Price = c.Attribute("price").Value,
            Company = c.Element("info").Attribute("companyname").Value,
            WebSite = c.Element("info").Attribute("website").Value
        }).ToList();

 

Rest of the program is almost same as previous program. Here is complete XAML code of our program.

Code Snippet
<Application x:Class="LinqXml.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

 

And here is complete C# coding of the program.

Code Snippet
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Xml.Linq;

namespace LinqXml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            String response = "<portfolio>" +
                                    "<stock shares=\"100\" symbol=\"MSFT\" price=\"$70.00\">" +
                                        "<info companyname=\"Microsoft Corporation\" website=\"http://www.microsoft.com\">&quot; +
                                        "</info>" +
                                    "</stock>" +
                                    "<stock shares=\"100\" symbol=\"AAPL\" price=\"$107.00\">" +
                                        "<info companyname=\"Apple Computer, Inc.\" website=\"http://www.apple.com\">&quot; +
                                        "</info>" +
                                    "</stock>" +
                                    "<stock shares=\"100\" symbol=\"DELL\" price=\"$50.00\">" +
                                        "<info companyname=\"Dell Corporation\" website=\"http://www.dell.com\">&quot; +
                                        "</info>" +
                                    "</stock>" +
                                    "<stock shares=\"100\" symbol=\"INTC\" price=\"$115.00\">" +
                                        "<info companyname=\"Intel Corporation\" website=\"http://www.intel.com\">&quot; +
                                        "</info>" +
                                    "</stock>" +
                                "</portfolio>";

            TextReader tr = new StringReader(response);
            XDocument doc = XDocument.Load(tr);

            var result = (from c in doc.Descendants("stock")
                    select new
                    {
                        Shares = c.Attribute("shares").Value,
                        Symbol = c.Attribute("symbol").Value,
                        Price = c.Attribute("price").Value,
                        Company = c.Element("info").Attribute("companyname").Value,
                        WebSite = c.Element("info").Attribute("website").Value
                    }).ToList();

            grid.ItemsSource = result;
        }
    }
}

 

This is the output of this program.

LinqXml

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

Categories

%d bloggers like this: