Friday 6 April 2012

multiple ways to read xml file elements:

if your xml :
<?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 follow are the methods :

1.
XDocument xdoc = XDocument.Parse(e.Result);
//var persons = xdoc.Descendants("people-search");
            //var per1 = persons.Descendants("people");
            //var per2 = per1.Descendants("person");
2.
//IEnumerable<XElement> persons = xdoc.Element("people-search").Elements("people").Elements("person");
3
 var persons = xdoc.Element("people-search").Elements("people").Elements("person");

No comments:

Post a Comment