.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Bind XML File to DataGrid in Silverlight

Posted By:Dhananjay Kumar       Posted Date: April 02, 2011    Points: 200    Category: Silverlight    URL: http://www.dotnetspark.com  

In this article, I have tried to demonstrate with simple steps how to Bind XML File to DataGrid in Silverlight

t is a common scenario when we need to bind or display data from a XML File to a Silverlight Data Grid. In this article, I have tried to demonstrate this with simple steps. What all we are going to do is:
  1. Download content of a XML file as string using the WebClient class.
  2. Parse the XML file using LINQ to XML.
  3. Bind the parsed result as the item source of a Data Grid.

Prepare Data Source

Put your XML File in the ClientBin folder. You can put the XML file at any server location but then you will have to provide an absolute URI of the XML file while downloading or reading the XML file.

I have created a sample Data.xml file: 



Design xaml page

I am going to create a simple page. This page is going to have a Button and a Data Grid. On the click event of the Button, Data from XML file will get bound to the Data Grid. 



Down Load Data from XML File

I am using the WebClient class to download the content of the XML file as a string. On the button click event the content of the XML file downloaded.


Do not forget to include the System.Net namespace to work with the WebClient class.

Parse XML Data and bind to Data Grid

We are going to use LINQ to XML to parse data. Include Namespace System.Xml.Linq. To parse data from a xml file, we have written the following function.


  1. Function is taking string as input parameter. Here we will pass e.Result from Downloadcompletedstring event.
  2. Creating an instance of XDocument by parsing string
  3. Reading each descendant or element on Xml file and assigning value of each attribute to properties of Entity class (Student).

We need to create an Entity class to map the data from the XML File. I am going to create a class Student with properties exactly as the same of attributes of the Student Element in XML file. 

Student .cs

namespace SilverlightApplication5
    public class Student
        public string RollNumber { get; set; }
        public string Name { get; set; }


For reference the full source code is as below:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq; 

namespace SilverlightApplication5
    public partial class MainPage : UserControl
        List lstStudents = null; 
        public MainPage()
            btnDemo.Click += new RoutedEventHandler(btnDemo_Click);

        private void btnDemo_Click(object sender, RoutedEventArgs e)
            WebClient client = new WebClient();
            Uri uritoXML = new Uri("Data.xml", UriKind.Relative);
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);


        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)

            if (e.Error != null)
                MessageBox.Show("There is Error Downloading Data from XML File ");


        void ParseXMLFile(string  dataInXmlFile)

            lstStudents = new List();

            XDocument xmlDoc = XDocument.Parse(dataInXmlFile);
            lstStudents  = (from r in xmlDoc.Descendants("Student")
                         select new Student
                             Name = (string) r.Attribute("Name").Value,
                             RollNumber =(string) r.Attribute("RollNumber").Value 

            grdXmlData.ItemsSource = lstStudents; 



On running we will get output as below: 


 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend