Friday 6 April 2012

reading xml file in silverlight and bind it to datagrid

if your xml looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<people-search>
  <people total="110" count="10" start="0">
    <person>
      <id>h2I51dNH9I</id>
      <first-name></first-name>
      <last-name>Private</last-name>
      <headline>Systems Engineer at Infosys Hyderabad</headline>
      <industry>Computer Software</industry>
      <location>
        <name>Hyderabad Area, India</name>
        <country>
          <code>in</code>
        </country>
      </location>
    </person>
    <person>
      <id>hGBdAqDDun</id>
      <first-name>Mohan</first-name>
      <last-name>Aswapuram</last-name>
      <picture-url>http://media.linkedin.com/mpr/mprx/0_QQ0tAHypVKsqUicjQhODAEDpZcS9BiFjXTIDAE0hb-M6ebrgo8fYxowanMDdqX6Ab5juODDW94fT</picture-url>
      <headline>PM at Infosys</headline>
      <industry>Computer Software</industry>
      <location>
        <name>Charlotte, North Carolina Area</name>
        <country>
          <code>us</code>
        </country>
      </location>
    </person>
    <person>
      <id>QQLtPL8D0W</id>
      <first-name>Deepali</first-name>
      <last-name>Singh</last-name>
      <picture-url>http://media.linkedin.com/mpr/mprx/0_M3jGd34jZdd3NigXMQgjdC4yNWsSN82XR_dgdCo3wumgwXRkzFDabGYfsFVtcbDHZG0p6LNE5n08</picture-url>
      <headline>Technology Lead at Infosys</headline>
      <industry>Information Technology and Services</industry>
      <location>
        <name>Bengaluru Area, India</name>
        <country>
          <code>in</code>
        </country>
      </location>
    </person>
    <person>
      <id>vrqwN9f2GF</id>
      <first-name>Navaneeth</first-name>
      <last-name>L.</last-name>
      <picture-url>http://media.linkedin.com/mpr/mprx/0_YFRwQKWA52NP4ojqyiwqQ1WDLaK_Z2yqp5fqQ-MAVIkPbewN-3IbHtD0I8ryRIg4xXJvWvF7YEw0</picture-url>
      <headline>Software Engineer with experience in Business Analysis (Banking) and testing</headline>
      <industry>Information Technology and Services</industry>
      <location>
        <name>Coimbatore Area, India</name>
        <country>
          <code>in</code>
        </country>
      </location>
    </person>
    <person>
      <id>zrmhI_VXKr</id>
      <first-name></first-name>
      <last-name>Private</last-name>
      <headline>Infrastrucuter support Analyst at JP Morgan</headline>
      <industry>Information Technology and Services</industry>
      <location>
        <name>United Kingdom</name>
        <country>
          <code>gb</code>
        </country>
      </location>
    </person>
    <person>
      <id>JoAswhqv3b</id>
      <first-name>Anuroudh</first-name>
      <last-name>Prakash</last-name>
      <headline>Technology Lead at Infosys Technologies Ltd</headline>
      <industry>Information Technology and Services</industry>
      <location>
        <name>Hyderabad Area, India</name>
        <country>
          <code>in</code>
        </country>
      </location>
    </person>
    <person>
      <id>Y-ldz8tUUp</id>
      <first-name>Manikandanprabhu</first-name>
      <last-name>Dharmaraj</last-name>
      <headline>Technology Analyst at Infosys</headline>
      <industry>Information Technology and Services</industry>
      <location>
        <name>Chennai Area, India</name>
        <country>
          <code>in</code>
        </country>
      </location>
    </person>
    <person>
      <id>CBurhsmRiB</id>
      <first-name>Binu</first-name>
      <last-name>K K</last-name>
      <picture-url>http://media.linkedin.com/mpr/mprx/0_Pk_hAcUAKBFpnqCgliLPAN2lrN8AqqigA_BxANg2flN3IKqj0CP2xqdGOVhaB1GltbTOO1_zowMB</picture-url>
      <headline>Senior Systems Engineer at Infosys Technologies Ltd</headline>
      <industry>Information Technology and Services</industry>
      <location>
        <name>Bengaluru Area, India</name>
        <country>
          <code>in</code>
        </country>
      </location>
    </person>
    <person>
      <id>l0nSRwiJkp</id>
      <first-name>Shilpi</first-name>
      <last-name>Parakh</last-name>
      <picture-url>http://media.linkedin.com/mpr/mprx/0_FiVC97OCRFovUCD1W5Rj92DTcXOJRCD15Gaj92mlHCodX6VPwLomZuVxJdY6Z5206h4yJwpeRf8P</picture-url>
      <headline>Warehouse Management Consultant - Retail clients</headline>
      <industry>Information Technology and Services</industry>
      <location>
        <name>London, United Kingdom</name>
        <country>
          <code>gb</code>
        </country>
      </location>
    </person>
    <person>
      <id>fBuSS_W5-u</id>
      <first-name></first-name>
      <last-name>Private</last-name>
      <headline>Group Project Manager at Infosys Technologies Limited</headline>
      <industry>Information Technology and Services</industry>
      <location>
        <name>Pune Area, India</name>
        <country>
          <code>in</code>
        </country>
      </location>
    </person>
  </people>
</people-search>

then first create a class for this :
public class Person
        {
            public string id { get; set; }
            public string firstname { get; set; }
            public string lastname { get; set; }
            public string headline { get; set; }
            public string industry { get; set; }
            public string location { get; set; }
            public string country { get; set; }
            public string url { get; set; }

            //public Uri sitestandardprofilerequest { get; set; }         
       
    }

then in the :
private void proxy_SearchForContactCompleted(object sender,SearchForContactCompletedEventArgs e)
        {
            XDocument xdoc = XDocument.Parse(e.Result);
            //ObservableCollection<Person> _PersonList = new ObservableCollection<Person>();
            List<Person> _PersonList = new List<Person>();
          
            string[] a = new string[] { };
            int i = 0;
            //var persons = xdoc.Descendants("people-search");//.Descendants("people-search")//.Elements("people-search")
            //var per1 = persons.Descendants("people");
            //var per2 = per1.Descendants("person");
          
            //IEnumerable<XElement> persons = xdoc.Element("people-search").Elements("people").Elements("person");
            var persons = xdoc.Element("people-search").Elements("people").Elements("person");
            foreach (var _persons in persons)
           
            {
                Person obj = new Person() { };

                //obj.id = _persons.Element("people").Element("person").Element("id").Value.ToString();
                obj.id = _persons.Element("id").Value.ToString();
                obj.firstname = _persons.Element("first-name").Value.ToString();
                obj.lastname = _persons.Element("last-name").Value.ToString();
                obj.headline = _persons.Element("headline").Value.ToString();
                obj.industry = _persons.Element("industry").Value.ToString();
                obj.location = _persons.Element("location").Element("name").Value.ToString();

                //obj.firstname = _persons.Element("people").Element("person").Element("first-name").Value.ToString();//_persons.Element("first-name").Value.ToString();
                //obj.lastname = _persons.Element("people").Element("person").Element("last-name").Value.ToString();//_persons.Element("last-name").Value.ToString();
                    //obj.headline = _persons.Element("people").Element("person").Element("headline").Value.ToString();//_persons.Element("headline").Value.ToString();
                    //obj.url = _persons.Element("site-standard-profile-request").Element("url").Value.ToString();
                    //obj.industry = _persons.Element("people").Element("person").Element("industry").Value.ToString();//_persons.Element("industry").Value.ToString();
                    //obj.location = _persons.Element("people").Element("person").Element("first-name").Value.ToString();//_persons.Element("location").Element("name").Value.ToString();
                    // obj.country = _persons.Element("people").Element("person").Element("first-name").Value.ToString();//_persons.Element("location").Element("country").Element("code").Value.ToString();
                    _PersonList.Add(obj);
                    //i++;
                    //_persons++;
               

            }
           

            dataGrid1.ItemsSource = _PersonList.ToArray();

        }

No comments:

Post a Comment