using XDocument with xpath to parse XML
which one is better in perfomance ?
e.g. To search for tag and get value in xml
tags = xmlDoc.Descendants(xmlTag);
or
xml.SelectSingleNode("//root/node")
So which one will be faster?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
XPath doesn’t parse XML… it’s a query language used on top of any other XML API which supports it. (For example, you can use XPath over
XmlDocumentorXDocument.)To find out which query would be faster usefully, you should try your actual XPath and LINQ to XML query on samples of your actual data. I would expect XPath to be faster in some situations, and LINQ to XML to be faster in others.
However, I’d be surprised if the query execution speed was actually the bottleneck in your code – do you have evidence that it is? You should first be asking yourself which is most readable. Implement that code, then see whether it’s fast enough.