views:

809

answers:

2

When do you use XPath over XmlDocument?

I'm trying to see things from a higher level. Is there a situation where it is better to use XPath over XmlDocument?

+2  A: 

When there is some reason to need an XmlDocument rather than an XPathDocument or XDocument, both of which support XPath with lighter weight in memory models.

XmlReader is good for reading a document linearly, but has not immediate support for XPath, but all the other three APIs do, but they vary with their support for other functionality. If you are looking to extract data consider XPathDocument (designed around XPath) or XDocument (enables LINQ to XML type approach) rather than XmlDocument.

Richard
+3  A: 

XPath and XmlDocument are not exclusive things. XPath is a language defined by W3C to allow XML documents to be queried using a path-like syntax. XmlDocument is a .NET class that abstracts away much regarding XML parsing. XmlDocument has methods such as SelectNodes and SelectSingleNode that take XPath expressions as parameters and return you the results.

I don't understand why you seem to think that XPath and XmlDocument can't be used together, or maybe I'm misunderstanding you completely.

Welbog