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(); 
4
  • 3
    The advantages of using XPath as compared to what? Would you otherwise iterate and search through nodes using the DOM, would you use LINQ to xml? What are you comparing XPath with? Commented Sep 10, 2009 at 22:14
  • Will it improve performance comparing to what? Commented Sep 10, 2009 at 22:15
  • At present I iterate through the node using a while of for loop. Thanks. Commented Sep 10, 2009 at 22:16
  • Sorry about the confusion, I will add a code sample to clarify. Thanks Commented Sep 10, 2009 at 22:20

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.