Thursday, September 16, 2010

C# XPATH vs LINQ

Use Xmlreader for parsing xml document.
Create an xml file as show below, but with 10000 contact elements called as “address_xw_n.xml” and make it available in D driver (d:\).
<AddressBook>
  <Contact>
    <Name>Person1</Name>
    <Address1><![CDATA[Number101]]></Address1>
    <Address2><![CDATA[Address501]]></Address2>
    <Zip>50001</Zip>
    <Phone><![CDATA[999991]]></Phone>
 </Contact>
</AddressBook>
The below code demonstrate the time take to query the above created xml file using xpath and LINQ for the Phone element value “999999996“.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.Xml.Linq;
namespace QueryXML
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch objSW = null;
            String strNormalFilePath;
            strNormalFilePath = @"d:\address_xw_n.xml";
            XmlDocument xmlNDoc;
            xmlNDoc = new XmlDocument();
            XDocument xNDoc;
            objSW = new Stopwatch();

            objSW.Reset();
            objSW.Start();
            //load the xml
            xmlNDoc.Load(strNormalFilePath);
            //xml query using xpath - Normal format
            objSW.Reset();
            objSW.Start();
            XmlNode xmlQNNode = xmlNDoc.SelectSingleNode(
                "/AddressBook/Contact/Phone[contains(text(),'999999996')]");
            //Console.WriteLine(xmlQNNode.Name +
            //  "-" + xmlQNNode.ChildNodes[0].Value);
            objSW.Stop();
            Console.WriteLine("XML query using xpath - Normal format");
            Console.WriteLine("TimeElapsed (Stopwatch float):{0}ms",
                objSW.Elapsed.TotalMilliseconds);
            Console.WriteLine("TimeElapsed (Stopwatch rounded):{0}ms",
                objSW.ElapsedMilliseconds);

            //xml query using LINQ - Normal format
            //Let us not include the conversion of XmlDocument to XDocument
            xNDoc = XDocument.Parse(xmlNDoc.OuterXml);
            objSW.Reset();
            objSW.Start();
            var linqNQuery = from p in
                                 xNDoc.Root.Elements("Contact").Elements("Phone")
                             where p.Value.Contains("999999996") select p;
            /*foreach (XElement e in linqNQuery)
            {
                Console.WriteLine(e.Name+"-"+e.Value);
            }*/
            objSW.Stop();
            Console.WriteLine("XML query using LINQ - Normal format");
            Console.WriteLine("TimeElapsed (Stopwatch float):{0}ms",
                objSW.Elapsed.TotalMilliseconds);
            Console.WriteLine("TimeElapsed (Stopwatch rounded):{0}ms",
                objSW.ElapsedMilliseconds);

            Console.ReadLine();
        }
    }
}

Output:
XML query using xpath - Normal format
TimeElapsed (Stopwatch float):20.0936ms
TimeElapsed (Stopwatch rounded):20ms
XML query using LINQ - Normal format
TimeElapsed (Stopwatch float):0.5082ms
TimeElapsed (Stopwatch rounded):0ms

Rule: Use LINQ for querying xml content.

7 comments:

gowsalya said...

Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.
python Training institute in Pune
python Training institute in Chennai
python Training institute in Bangalore

haripriya said...

A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
Data Science Training in Indira nagar
Data Science Training in btm layout
Data Science Training in Kalyan nagar
Data Science training in Indira nagar
Data Science Training in Marathahalli

nisha said...

The Blog is really very Impressive. every concept of this blog is explained very neatly.

Data Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery

Revathi said...

Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website..keep it up!!

android training in chennai

android online training in chennai

android training in bangalore

android training in hyderabad

android Training in coimbatore

android training

android online training

jeni said...

Your technical information related with java programming is very useful and interesting. Also share updated details about java in your website. Thanks for sharing this article.

sap training in chennai

sap training in velachery

azure training in chennai

azure training in velachery

cyber security course in chennai

cyber security course in velachery

ethical hacking course in chennai

ethical hacking course in velachery

Reshma said...

Such a great blog.Thanks for sharing useful information......
Azure Training in Bangalore
Microsoft Azure training in Bangalore

David Fincher said...

Great post. Thanks for sharing such a useful blog.
Salesforce Training in T Nagar
Salesforce Training in Chennai