Thursday, 5 April 2012

bind a xml result into datagrid in silverlight

first check : AutoGenerateColumns="True" in datagrid.

if you want to do in web services or places:

XmlDocument xml = new XmlDocument();
         XmlNodeList xnList = xml.SelectNodes("/person");

         //int[] ids = new int[10];
         //string[] str = new string[10];
         //int i = 0;
         string firstName="", lastName="", headline="";
         foreach (XmlNode xn in xnList)
              firstName = xn["first-name"].InnerText;
              lastName = xn["last-name"].InnerText;
              headline = xn["headline"].InnerText;
             //string url = xn["url"].InnerText;
             //Console.WriteLine("Name: {0} {1}", firstName, lastName);

XmlDocument will work.

if you want to do in silverlight scree:
first check the result which fields are coming in result.

if you are getting result like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

then we need to print this xml in datagrid:

for this first create on class function in silverlight screen:

   public class Person
            public string firstname { get; set; }
            public string lastname { get; set; }
            public string headline { get; set; }
            public string url { get; set; }
            //public Uri sitestandardprofilerequest { get; set; }        

then :

XDocument xdoc = XDocument.Parse(e.Result);
            List<Person> _PersonList = new List<Person>(10);
            Person obj = new Person();
            string[] a = new string[] { };
            int i=0;
            var persons = from d in xdoc.Elements("person")
                        select d;
            foreach (var _persons in persons)
                obj.lastname = _persons.Element("last-name").Value.ToString();
                obj.headline = _persons.Element("headline").Value.ToString();
                obj.url = _persons.Element("site-standard-profile-request").Element("url").Value.ToString();

            dataGrid1.ItemsSource = _PersonList;

you will get the result in grid view.

No comments:

Post a Comment