Absolute Thoughts....

  Home  |   Contact  |   Syndication    |   Login
  9 Posts | 0 Stories | 12 Comments | 0 Trackbacks

News

Archives

Lets take a sample XML file and name it as Meeting.xml.

<?xml version="1.0" encoding="utf-8"?>
<billing>
  <item>
    <id>1</id>
    <customer>Pavan Kumar Pabothu</customer>
    <type>Daily Scrum Call</type>
    <date>4/5/2012</date>
    <hours>1</hours>
    <contact>
       <phone>
        <no>979107</no>
        <type>home</type>
      </phone>
      <phone>
        <no>9999995929</no>
        <type>mobile</type>
      </phone>
    </contact>
  </item>
  <item>
    <id>2</id>
    <customer>Reddaiah Raju</customer>
    <type>Sprint Meeting</type>
    <date>3/20/2014</date>
    <hours>4</hours>
    <contact>
       <phone>
        <no>2375226</no>
        <type>home</type>
      </phone>
      <phone>
        <no>998884888</no>
        <type>mobile</type>
      </phone>
    </contact>
  </item>
  <item>
    <id>3</id>
    <customer>Kishan Chand Yadav</customer>
    <type>Daily Status Call</type>
    <date>4/20/2014</date>
    <hours>1</hours>
    <contact>
       <phone>
        <no>9988776652</no>
        <type>home</type>
      </phone>
      <phone>
        <no>9988776654</no>
        <type>mobile</type>
      </phone>
    </contact>
  </item>
</billing>

For the above XML we need to create entity objects as below

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string MeetingType { get; set; }
    public DateTime CallDate { get; set; }
    public int DurationInHours { get; set; }
    public Contact Contacts { get; set; }
}

public class Contact
{
    public List<PhoneContact> Phone = new List<PhoneContact>();
}

public class PhoneContact
{
    public string Type { get; set; }
    public string Number { get; set; }
}

And we can read the XML using below code.

// Using LINQ
private List<Customer> GetCustomerList()
{
    XElement xmlDoc = XElement.Load(Server.MapPath(@"~/Data/Meeting.xml"));
    var customers =
     from cust in xmlDoc.Descendants("item")
     select new Customer
     {
     ID = Convert.ToInt32(cust.Element("id").Value),
     Name = cust.Element("customer").Value,
     MeetingType = cust.Element("type").Value,
     CallDate = Convert.ToDateTime(cust.Element("date").Value),
     DurationInHours = Convert.ToInt32(cust.Element("hours").Value),
     Contacts = new Contact()
     {
     Phone = new List<PhoneContact>(from phn in cust.Descendants("phone")
select new PhoneContact
{
   Type = phn.Element("type").Value,
     Number = phn.Element("no").Value
})
     }
     };
    return customers.ToList();
}


// Using LAMDA
private List<Customer> GetCustomerList()
{
XElement xmlDoc = XElement.Load(Server.MapPath(@"~/Data/Meeting.xml"));

var customers =
xmlDoc.Descendants("item").Select(cust => new Customer
{
ID = Convert.ToInt32(cust.Element("id").Value),
Name = cust.Element("customer").Value,
MeetingType = cust.Element("type").Value,
CallDate = Convert.ToDateTime(cust.Element("date").Value),
DurationInHours = Convert.ToInt32(cust.Element("hours").Value),
Contacts = new Contact { 
Phone = cust.Descendants("phone").Select(phn => new PhoneContact { 
Number = phn.Element("type").Value, 
Type = phn.Element("no").Value 
}).ToList()
}
});

return customers.ToList();
}


posted on Tuesday, April 29, 2014 10:33 PM

Feedback

# re: Reading a complex XML using LINQ in C# 11/21/2014 12:22 AM RK
This was very helpful. Thanks for posting.

# re: Reading a complex XML using LINQ in C# 6/19/2015 10:06 PM D
Very good! Exactly what I needed

# re: Reading a complex XML using LINQ in C# 1/19/2016 9:39 PM Akın
Thanks man, save to my day.

Post A Comment
Title:
Name:
Email:
Comment:
Verification: