views:

771

answers:

3

I am refactoring some code in an existing system. The goal is to remove all instances of the XmlDocument to reduce the memory footprint. However, we use XPath to manipulate the xml when certain rules apply. Is there a way to use XPath without using a class that loads the entire document into memory? We've replaced all other instances with XmlTextReader, but those only worked because there is no XPath and the reading is very simple.

Some of the XPath uses values of other nodes to base its decision on. For instance, the value of the message node may be based on the value of the amount node, so there is a need to access multiple nodes at one time.

+2  A: 

If your XPATH expression is based on accessing multiple nodes, you're just going to have to read the XML into a DOM. Two things, though. First, you don't have to read all of it into a DOM, just the part you're querying. Second, which DOM you use makes a difference; XPathDocument is read-only and tuned for XPATH query speed, unlike the more general purpose but expensive XmlDocument.

Steven Sudit
+1  A: 

I supose that using System.Xml.Linq.XDocument is also prohibited? Otherwise, it would be a good choice, as it is faster than XmlDocument (as I remember).

Philippe
Everything is faster than XmlDocument. :-)
Steven Sudit
However, since it allows changing the XML, it's likely to be slower than XPathDocument.
Steven Sudit
Yup, probably, but the title states that it needs to read/write/xpath, so I guess XPathDocument will not do.
Philippe
I'm not opposed to reading the xml and writing to another xml. It's just not as elegant.
Brian
A: 

Supporting XPath means supporting queries like:

//address[/states/state[@code=current()/@code]='California']

or

//item[@id != preceding-sibling/item/@id]

which require the XPath processor to be able to look everywhere in the document. You're not going to find a forward-only XPath processor.

Robert Rossney