Posts Tagged C#

LINQ to XML


LINQ to XML

LINQ(Language Integrated Query)to XML is the modern way of dealing with XML data. The Linq to Xml came with the.Net Framework 3.5. Before that we were using XMLDocument which follows the DOM architecture. But with the introduction of LINQ the task has been reduced a lot coding wise for the developers. But for type of operations the whole XML Document has to be loaded in memory.

Today we will look how we canperform certain operation on Xml data using the Linq to XML architecture.

Reading the Xml and Storing in List

Lets create a sample XML which we are going to use read using Linq to XML feature. The below XML is our sample xml lets name it as “Sample.xml”.

<Configurations>
    <Employee Id="1">
        <Info Name="aaaaa" Age="24" Sex="Male"></Info>
        <Info Name="bbbbb" Age="25" Sex="Male"></Info>
    </Employee>
    <Employee Id="2">
        <Info Name="ccccc" Age="26" Sex="Male"></Info>
        <Info Name="ddddd" Age="27" Sex="Female"></Info>
    </Employee>
</Configurations>

Lets create a class a define some

property that can hold the xml values. Lets name this class as ReadingXmlWithLinq.cs

public class ReadingXmlWithLinq
        {
            public string Name { get; set; }
            public string Age { get; set; }
            public string Sex { get; set; }
        }

Now let’s see how we can read the Sample.xml file using Linq to Xml concept.

using System;
using System.Linq;
using System.Xml.Linq;

private IList<ReadingXmlWithLinq> GetXMlList()

{	
   XDocument configparentXML = XDocument.Load(xmlPathAddress);
   string node = "Info";
   var configs = (from config in configparentXML.Descendants(node)
   select new ReadingXmlWithLinq
    {
       Name = config.Attribute("Name") != null ? config.Attribute("Name").Value : "",
       Age = config.Attribute("Age") != null ? config.Attribute("Age").Value : datatable,
       Sex = config.Attribute("Sex") != null ? config.Attribute("Sex").Value : "",
     });
   IList<ReadingXmlWithLinq> rules = configs.ToList();
   return rules;
}

Now this will return a list of four elements. Thus we will get all the four rows values. Thus rules[0].Name will give “aaaaa” and rules[3].Name will give “ddddd” Now what if I need only those info whose Id is 1. That is Node Attribute value Id. So now this looks little  difficult. But if we are using Linq it is quite simple. Lets see

private IList<ReadingXmlWithLinq> GetXMlList()
        {
            XDocument parentXML = XDocument.Load(xmlPathAddress);
            string node = Employee;
            string IdValue = "1";

            var parentconfigs = from config in parentXML.Descendants(node)
                                where (string)config.Attribute("Id") == IdValue
                                select config;
            string childnode = "Info";
            configs = (from config in parentconfigs.Descendants(childnode)
                       select new ReadingXmlWithLinq
                       {
                           Name = config.Attribute("Name") != null ? config.Attribute("Name").Value : "",
                           Age = config.Attribute("Age") != null ? config.Attribute("Age").Value : datatable,
                           Sex = config.Attribute("Sex") != null ? config.Attribute("Sex").Value : "",
                       });

            IList<ReadingXmlWithLinq> rules = configs.ToList();
            return rules;
        }

Now the above function will return only two values that is Employee ID’s value equal to 1. Thus we can get selective value based on certain criteria or condition. This is pretty simple and easy as compared to the traditional way of handling. 

Advertisements

, , ,

Leave a comment