tags:

views:

237

answers:

1

I am planning to use XPath to query an XML file. Can you please point me to a link which states the advantages of using XPath? Will the use of XPath improve performance?

I am using .NET Framework 2.0. At present I am iterating through the node.

XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
XmlReader reader = XmlReader.Create("mydata.xml", settings);
XmlReader inner;
while (reader.Read())
{
if (reader.Name == "xyz" && reader.NodeType == XmlNodeType.Element)
{
    inner = reader.ReadSubtree();
    inner.Skip();
    inner.Read();
    ......... //some changes to node
    inner.Close();
}
}
reader.Close();
+3  A: 

I've heard great things about LINQ to XML. It appears to perform even better than xPath according to the blog.dreamlabsolutions.com link below.

Code Sample on LINQ to XML Query from blogs.block4.net:

var query = from c in xmlFile.Elements("book")
            where (string)c.Attribute("checked-out").Value == "false"            
            select c;

foreach (var book in query)
{
   Console.WriteLine("\”{0}\” is available", book.Element("title").Value);
}

Further Reading:
* xml.com
* weblogs.asp.net
* blog.dreamlabsolutions.com

RSolberg
Thanks a lot for your answer. Is it possible or advisable to use LINQ in .NET Framework 2.0. Thanks
kobra
Yes. System.Data.Linq is the assembly
RSolberg
LINQ can make your code much more readable/maintainable (not to mention easier to write). If performance is adequate, I'd opt for maintainability.
TrueWill
@TrueWill: Agree 100%
RSolberg
Thanks a lot for your help. I would have voted for you @RSolberg, but at present don't have 15 reputations.
kobra
Thanks @RSolberg, I have voted for your answer. Thanks for the help.
kobra