Sunday, 2 February 2014

LINQ - Deserialize Complex xml with Elements and Attributes With Simple C# Code

Lets Have Xml like Below:

<?xml version="1.0" encoding="utf-8"?>
<Class>
<Twelth-A>
  <Quaterly Name="Vignesh" Lang="80" Maths="90" Science="85" History="92" Social="78"/>
  <Quaterly Name="Karthik" Lang="96" Maths="99" Science="84" History="87" Social="90"/>
  <Quaterly Name="Rosan" Lang="67" Maths="89" Science="37" History="78" Social="56"/>
  <Halerly Name="Vignesh" Lang="87" Maths="98" Science="85" History="58" Social="98"/>
  <Halerly Name="Karthik" Lang="96" Maths="67" Science="84" History="98" Social="89"/>
  <Halerly Name="Rosan" Lang="87" Maths="79" Science="56" History="78" Social="56"/>
  <Annual Name="Vignesh" Lang="89" Maths="90" Science="85" History="92" Social="78"/>
  <Annual Name="Karthik" Lang="90" Maths="99" Science="87" History="97" Social="88"/>
  <Annual Name="Rosan" Lang="78" Maths="78" Science="37" History="70" Social="98"/>
</Twelth-A>
<Twelth-B>
  <Quaterly Name="Khan" Lang="80" Maths="90" Science="85" History="92" Social="78"/>
  <Quaterly Name="Rao" Lang="96" Maths="99" Science="84" History="87" Social="90"/>
  <Quaterly Name="Ritu" Lang="67" Maths="89" Science="37" History="78" Social="56"/>
  <Halerly Name="Khan" Lang="87" Maths="98" Science="85" History="58" Social="98"/>
  <Halerly Name="Rao" Lang="96" Maths="67" Science="84" History="98" Social="89"/>
  <Halerly Name="Ritu" Lang="87" Maths="79" Science="56" History="78" Social="56"/>
  <Annual Name="Khan" Lang="89" Maths="90" Science="85" History="92" Social="78"/>
  <Annual Name="Rao" Lang="90" Maths="99" Science="87" History="97" Social="88"/>
  <Annual Name="Ritu" Lang="78" Maths="78" Science="37" History="70" Social="98"/>
</Twelth-B>
</Class>

C# Code To Deserialize This Using Linq

//Static Function
static void Main(string[] args)
        {
            string PreviousExamName = string.Empty;
            var xDoc = XDocument.Load("G:\\MyApplicaton\\testxml.xml");

            //To Read All the Elements And Its Attributes
            var values = (from r in xDoc.Descendants("Class")
                          from a in r.Elements()
                          select a
             ).ToList();

            //Get Each Elements Seperately under parent Node
            foreach (XElement n in values)
            {
                string ClassName ="Class " + n.Name.ToString();
                var results = (from node in n.Descendants()
                               select node).ToList();
                Console.WriteLine(ClassName);
                string header = string.Empty;

                //Get Each Elements Seperately
                foreach (XElement node in results)
                {
                    string Exam = "Exam " + node.Name.ToString();

                    //Get The all Attribute Seperately
                    var Marks = (from a in node.Attributes()
                                   select a).ToList();
                    if (PreviousExamName != Exam)
                    {
                        header = string.Empty;
                        Console.WriteLine();
                        Console.WriteLine(Exam);
                    }
                    PreviousExamName = Exam;
                    string sub = string.Empty;
                    string mark = string.Empty;

                    //Get the each Attribute Seperately
                    foreach (XAttribute s in Marks)
                    {
                        if (header == string.Empty)
                        {
                            sub += s.Name.LocalName + "\t";
                        }
                        mark += s.Value + "\t";
                    }
                    if (header == string.Empty)
                    {
                        Console.WriteLine(sub);
                    }
                    Console.WriteLine(mark);
                    header = "NE";
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }

You can Print the Result Like below




No comments:

Post a Comment