Reading Complex XML Using LINQ

XML Data

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <company>  
  3.     <Emp_details>  
  4.         <id>1</id>  
  5.         <emp>satyam</emp>  
  6.         <emp_type>Developer</emp_type>  
  7.         <joining_date>4/5/2012</joining_date>  
  8.         <contact>  
  9.             <phone>  
  10.                 <no>98924567</no>  
  11.                 <type>home</type>  
  12.             </phone>  
  13.             <phone>  
  14.                 <no>98564786</no>  
  15.                 <type>mobile</type>  
  16.             </phone>  
  17.         </contact>  
  18.     </Emp_details>  
  19.     <Emp_details>  
  20.         <id>2</id>  
  21.         <emp_name>anil</emp_name>  
  22.         <emp_type>Developer</emp_type>  
  23.         <joining_date>4/5/2012</joining_date>  
  24.         <contact>  
  25.             <phone>  
  26.                 <no>345678</no>  
  27.                 <type>home</type>  
  28.             </phone>  
  29.             <phone>  
  30.                 <no>9856555554786</no>  
  31.                 <type>mobile</type>  
  32.             </phone>  
  33.         </contact>  
  34.     </Emp_details>  
  35.     <Emp_details>  
  36.         <id>3</id>  
  37.         <emp>nandan</emp>  
  38.         <emp_type>Developer</emp_type>  
  39.         <joining_date>4/5/2012</joining_date>  
  40.         <contact>  
  41.             <phone>  
  42.                 <no>98924568767</no>  
  43.                 <type>home</type>  
  44.             </phone>  
  45.             <phone>  
  46.                 <no>98564234786</no>  
  47.                 <type>mobile</type>  
  48.             </phone>  
  49.         </contact>  
  50.     </Emp_details>  
  51.   
  52. </company>  
CS File

  1. public class Emp  
  2. {  
  3.     public int ID   
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string Emp_Name  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string EmpType  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public DateTime join_date  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23.     public Contact Contacts  
  24.     {  
  25.         get;  
  26.         set;  
  27.     }  
  28. }  
  29.   
  30. public class Contact  
  31. {  
  32.     public List < PhoneContact > Phone = new List < PhoneContact > ();  
  33. }  
  34.   
  35. public class PhoneContact  
  36. {  
  37.     public string Type  
  38.   {  
  39.         get;  
  40.         set;  
  41.     }  
  42.     public string Number  
  43.     {  
  44.         get;  
  45.         set;  
  46.     }  
  47. }  
  48.   
  49.   
  50.   
  51.   
  52. // Using LINQ  
  53. private List < Emp > GetempList()  
  54. {  
  55.         XElement xmlDoc = XElement.Load(Server.MapPath(@ "~/Data/Meeting.xml))  
  56.                     var Employee = from cust in xmlDoc.Descendants("Emp_details")  
  57.                     select new Emp  
  58.                      {  
  59.                         ID = Convert.ToInt32(cust.Element("id").Value),  
  60.                             Emp_Name = cust.Element("emp_name").Value,  
  61.                             EmpType = cust.Element("emp_type").Value,  
  62.                             join_date = Convert.ToDateTime(cust.Element("joining_date").Value),  
  63.                             Contacts = new Contact()  
  64.                                 {  
  65.                                 Phone = new List < PhoneContact > (from phn in cust.Descendants("phone") select new PhoneContact {  
  66.                                     Type = phn.Element("type").Value,  
  67.                                         Number = phn.Element("no").Value  
  68.                                 })  
  69.                             }  
  70.                     };  
  71.                     return Employee.ToList();  
  72.                 }  
  73.   
  74.   
  75.                 // Using LAMDA Expression  
  76.                 private List < Customer > GetempList()   
  77.                 {  
  78.                     XElement xmlDoc = XElement.Load(Server.MapPath(@ "~/Data/Meeting.xml))  
  79.   
  80.                                 var Employee =  
  81.                                     xmlDoc.Descendants("item").Select(cust => new Customer  
  82.                                      {  
  83.                                         ID = Convert.ToInt32(cust.Element("id").Value),  
  84.                                             Emp_Name = cust.Element("emp_name").Value,  
  85.                                             EmpType = cust.Element("emp_type").Value,  
  86.                                             join_date = Convert.ToDateTime(cust.Element("joining_date").Value),  
  87.                                             Contacts = new Contact  
  88.                                             {  
  89.                                                 Phone = cust.Descendants("phone").Select(phn => new PhoneContact  
  90.                                                 {  
  91.                                                     Number = phn.Element("type").Value,  
  92.                                                         Type = phn.Element("no").Value  
  93.                                                 }).ToList()  
  94.                                             }  
  95.                                     });  
  96.   
  97.                                 return Employee.ToList();  
  98.                             }