It 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:
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<Student> lstStudents = null; public MainPage() { InitializeComponent(); 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); client.DownloadStringAsync(uritoXML); } void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show("There is Error Downloading Data from XML File "); } else { ParseXMLFile(e.Result); } } void ParseXMLFile(string dataInXmlFile) { lstStudents = new List<Student>(); 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 }).ToList(); grdXmlData.ItemsSource = lstStudents; } }}On running we will get output as below:
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: