Friday, November 15, 2013

Complex Structured XML Nodes and Attributes reading using LINQ

In this article i will be using examples to show the ways to read a complex structured XML nodes and attributes value using LINQ. 

As you are aware of the powerful features LINQ provides, and the best thing about LINQ is simple and easy way to use it. 

I have created a customerconfig.xml XML file, which contains following details

  • 1st Section -> Source Feed Info (Feed file placed in a shared location)

    • CSV File path placed in a shared location
    • CSV File Name
    • Data demiliter of the CSV File

  • 2nd Section -> Column names, datatype and index position (available in the csv file)
    • First_Name
    • Last_Name
    • DOB
    • EMP_No

<?xml version="1.0" encoding="utf-8"?>
  <CustomerFeedInfo IsActive="1">
      <Column Name="First_Name" Index="0" DataType="String"/>
      <Column Name="Last_Name" Index="1" DataType="String"/>
      <Column Name="DOB" Index="2" DataType="DateTime"/>
      <Column Name="Emp_No" Index="3" DataType="Int"/>

I am using C#.Net, .Net Framework 4.0 and VS 2010.

Lets start with the coding to read the node and attribute values from different sections of the configuation xml.

//first assigning the xml file path to a variable
string path = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\bin\Debug", @"");          
path = path + "/CustomerConfig.xml";

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

XDocument xDoc = XDocument.Load(path);

//LINQ query to read the xml file details
var feedDetails = from feed in xDoc.Descendants("CustomerFeedInfo")
                  where feed.Attribute("IsActive").Value == "1"
                  select new
                     SourceInfo = feed.Descendants("SourceInfo").Descendants(),
                     ColumnInfo = feed.Descendants("ColumnDetails").Descendants()

//using foreach to loop through all the items available in feedDetails
foreach (var feed in feedDetails)
   //Config file source details reading 

   //Index based sourceinfo retrieval
   string separator = feed.SourceInfo.ElementAt(0).Value;
   string filepath = feed.SourceInfo.ElementAt(1).Value;
   string fileName = feed.SourceInfo.ElementAt(2).Value;

   //foreach loop for sourceinfo retrieval 
   Console.WriteLine("Source Info Details");
                foreach (XElement xe in feed.SourceInfo)
                    Console.WriteLine(xe.Name+" = "+ xe.Value);


   //column details retrieval
   Console.WriteLine("Column Details");

   //using index position
   string sColumn = feed.ColumnInfo.ElementAt(0).Attribute("Name").Value;
   string sColumn1 = feed.ColumnInfo.ElementAt(1).Attribute("Name").Value;
   string sColumn2 = feed.ColumnInfo.ElementAt(2).Attribute("Name").Value;

   //using foreach loop to retrieve the column details
   foreach (XElement xe in feed.ColumnInfo)
       Console.WriteLine(xe.Attribute("Name").ToString()+"="+ xe.Attribute("Name").Value);


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

Happy Coding.....

1 comment :

  1. Sir is there a loop that will print all the elements and attributes into one table?