Reading XML values using LINQ

Today i will be using examples to show the ways to read XML nodes and attributes value using LINQ. Everyone is aware of the powerful features LINQ provides, and the best thing about LINQ is simple and easy way to use it.

Step 1:-

I have created a customer.xml file, which contains the details about the customer. Ex:- Name, CompanyName, JobTitle etc.

<?xml version="1.0" encoding="utf-8" ?>
<Customers>
  <Customer Id="1">
    <Name>B. K. Mishra</Name>
    <CompanyName>FIC Limited</CompanyName>
    <JobTitle>Sales Representative</JobTitle>
    <Address>1st Block, 19th cross,sector 7</Address>
    <City>Bangalore</City>
    <Country>India</Country>
    <Fax>080-98765457</Fax>
    <EmailId>B.k.mishra@fic.com</EmailId>
  </Customer>
  <Customer Id="2">
    <Name>A. K. Mitra</Name>
    <CompanyName>Sourcesoft Ltd</CompanyName>
    <JobTitle>Maketing Manager</JobTitle>
    <Address>Building no-10, second floor,1st block</Address>
    <City>Delhi</City>
    <Country>India</Country>
    <Fax>011-23456789</Fax>
    <EmailId>AKMitra@sourcesoft.com</EmailId>
  </Customer>
  <Customer Id="3">
    <Name>S. Reddy</Name>
    <CompanyName>Edge solution</CompanyName>
    <JobTitle>Sales Representative</JobTitle>
    <Address>Raheja arcade,3rd floor, sector 1</Address>
    <City>Bangalore</City>
    <Country>India</Country>
    <Fax>080-22224455</Fax>
    <EmailId>SReddy@EdgeSol.com</EmailId>
  </Customer>
  <Customer Id="4">
    <Name>K. Manakotte</Name>
    <CompanyName>Info Ltd</CompanyName>
    <JobTitle>Marketing Manager</JobTitle>
    <Address>Citi tower, MG Road</Address>
    <City>Bangalore</City>
    <Country>India</Country>
    <Fax>080-20224554</Fax>
    <EmailId>k.manakotte@info.com</EmailId>
  </Customer>
</Customers>

Step 2:

XDocument class represents a XML document. XDocument.Load method Creates a new System.Xml.Linq.XDocument object from a file.

(i) Following query will be used to retrieve all the customer details from the xml document
    
    //retrieving all the customer details
    var customerList =from cust in xDoc.Descendants("Customer")
                      select cust;
    foreach (var cust in customerList)
    {
      //writing output to the console output window
      Console.WriteLine(cust.Element("Name").Value + ";" +  
                        cust.Element("CompanyName").Value+";"+cust.Element("JobTitle").Value  
                        + ";"+cust.Element("Fax").Value + ";" +cust.Element("EmailId").Value
                       );
    }

(ii) In this query anonymous class will be used to retrieve all the customer details from the xml document.
     
     "Anonymous types typically are used in the select clause of a query expression to return a subset of the
   properties from each object in the source sequence."
     
     If we do not specify member names in the anonymous type, the compiler gives the 
     anonymous type members the same name as the property being used to initialize them.

    Using Anonymous type, custom name can be assigned to the properties to select values.

    var custList = from cust in xDoc.Descendants("Customer")
                             select new
                                {
                                    CustomerName = cust.Element("Name").Value,
                                    JobTitle = cust.Element("JobTitle").Value,
                                    Fax = cust.Element("Fax").Value,
                                    EmailId = cust.Element("EmailId").Value
                                };
    foreach (var cust in custList)
    {
        Console.WriteLine(cust.CustomerName+";"+cust.JobTitle+";"+cust.Fax+";"+
        cust.EmailId);
    }

(iii) Retrieving all the records where city=Bangalore
    
  var blrCustomer = from custList in xDoc.Descendants("Customer")
                                     where custList.Element("City").Value == "Bangalore"
                                     select new
                                     {
                                             CustomerName = custList.Element("Name").Value,
                                            JobTitle = custList.Element("JobTitle").Value,
                                            Fax = custList.Element("Fax").Value,
                                            EmailID = custList.Element("EmailId").Value
                                      };
    foreach (var cust in blrCustomer)
    {
         Console.WriteLine(cust.CustomerName + ";" + cust.JobTitle + ";" + cust.Fax + ";"
                                           cust.EmailID);
    }


(iv) Retrieving all the emailids from the xml document

    var custEmailIDList = from custList in xDoc.Descendants("Customer")
                                           select new {EmailID=custList.Element("EmailId").Value};

    foreach (var email in custEmailIDList)
    {
         Console.WriteLine(email.EmailID);
    }

(v) Counting the length of character in name and then retrieving the customer records        matching with the maximum value.
    
    var custLength = from custList in xDoc.Descendants("Customer")
                                   where custList.Element("Name").Value.Length== 
                                                               ( from n in xDoc.Descendants("Customer")
                                                                  orderby n.Element("Name").Value.Length descending
                                                                  select n.Element("Name").Value.Length).First()
                                  select new
                                             {
                                                     CustomerName = custList.Element("Name").Value,
                                                     JobTitle = custList.Element("JobTitle").Value,
                                                     Fax = custList.Element("Fax").Value,
                                                     EmailID = custList.Element("EmailId").Value
                                             };
   foreach (var cust in custLength)
   {
        Console.WriteLine(cust.CustomerName + ";" + cust.JobTitle + ";" + cust.Fax + ";"
                                         cust.EmailID);
   }

As you can see from the output that only those names, of which length is matching with the name of maximum length is displayed in the output.

I hope these examples gives clear idea about LINQ to XML features.

Happy Coding.....

Comments