Saturday, May 25, 2013

Reading a complex XML file using LinQ [Solved]

This sample code will help you to parse XML data using LinQ C# . The XML file has a lot of levels, I capture all value in one ILIST<> . there are more tags in items.
The XML has the following syntax:

  
    
      19
      2
      Guatemala
      502
      GT
      TIGO
      7.0
    
    
      20
      2
      Guatemala
      502
      GT
      TIGO
      10.0
    
    
      21
      2
      Guatemala
      502
      GT
      TIGO
      14.0
    
  

Just follow this code to Read XML.
using System.Xml.XPath;
...

var chartrate = from a in xmlDoc.XPathSelectElements ("/root/items/*")

                     select new 
                     {
                         Product = a.Element("product_id").Value,
                         Menu= a.Element("menu_rank").Value,
                         Country= a.Element("menu_country").Value

                     };
If you have any query, feel free to get in touch please.