Reading XML nodes Attributes values using LINQ in C#

Recently i came across a requirement where i was supposed to read a XML file. But this time the requirement was to read the values from XML file which were declared as Attributes in XML node as opposed to XML node child elements.

In today's example i will be sharing the solution with the use of LINQ in C# for achieving the same results with a sample xml.

Customers.xml


<?xml version="1.0" encoding="utf-8" ?>
<Customers>
  <customer name="Steve John" Id="CUST0001" location="New York" country="United                             States" DOB="12/23/1960" Phone="+1 212 666 1111" />
  <customer name="Sagar Mehta" Id="CUST0106" location="Bangalore" country="India" DOB="07/23/1978" Phone="+91 80 8888 1001" />
  <customer name="Rahul Raj" Id="CUST0190" location="Delhi" country="India" DOB="01/31/1980" Phone="+91 11 6666 1111" />
  <customer name="John Roderix" Id="CUST0301" location="Sydney" country="Australia" DOB="02/13/1956" Phone="+61 (0)2 8062 3999" />
</Customers>

Loading the customers xml into the XDocument object:

XDocument xDoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory+ "/Customers.xml");


Requirement 1: Retrieving all the customers details in Attributes Name,value format whose details match the city="Bangalore"

IEnumerable<XElement> rows = from row in xDoc.Descendants("customer")
                where (string)row.Attribute("country") == "India" 
                select row;

//looping through the XElement details one by one
foreach (XElement ele in rows)
{
 //for the current xElement retrieving all the Attributes details
 IEnumerable<XAttribute> attList = 
                from att in ele.DescendantsAndSelf().Attributes()
                select att;

 //Looping through attributes list and displaying it on the screen
   foreach (XAttribute att in attList)
   {
         Console.WriteLine(att);
   }

   //Just to show a separator between two customer details
   Console.WriteLine("---First customer details ends here ----");
}


Requirement 2: Retrieving all the customers details in Attributes Name,value format whose details match the city="Bangalore" and country ="India"

IEnumerable<XElement> rows = 
              from row in xDoc.Descendants("customer")
              where (string)row.Attribute("country") == "India"
                    (string)row.Attribute("city") == "Bangalore"
              select row;
//looping through the XElement details one by one
foreach (XElement ele in rows)
{
 //for the current XElement retrieving all the Attributes details
 IEnumerable<XAttribute> attList = 
              from att in ele.DescendantsAndSelf().Attributes()
              select att;

//Looping through attributes list and displaying it on the screen
 foreach (XAttribute att in attList)
 {
    Console.WriteLine(att);
 }
}


Requirement 3: Retrieving all the customers details as Attribute value whose details match the city="Sydney" and 
country ="Australia"

IEnumerable<XElement> rows = 
            from row in xDoc.Descendants("customer")
            where (string)row.Attribute("country") == "Australia" 
                & (string)row.Attribute("city") == "Sydney"
            select row;

//looping through the XElement details one by one
foreach (XElement ele in rows)
{
 //for the current XElement retrieving all the Attributes details
 IEnumerable<XAttribute> attList = 
               from att in ele.DescendantsAndSelf().Attributes()
               select att;

//Looping through attributes list and displaying it on the screen
 foreach (XAttribute att in attList)
 {
    //retrieving the attribute value
    Console.WriteLine(att.Value);
 }
}

I have used Console.Write for writing the output to demonstrate the output retrieved. You can use any way the data is required to be displayed or stored in a collection.


Happy Coding.........


Comments

  1. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. Dot Net Training in chennai | dot net training institute in velachery

    ReplyDelete
  2. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.. dot net training in chennai | best dot net training in chennai

    ReplyDelete

Post a Comment