Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPathDocument vs. XmlDocument

I'm developing a blogging engine in ASP.NET and one the repositories is implemented to use XML files as the data store. the XML repository will mainly be used for local use and testing purposes. now although it might not be that of an issue with today's computers which have lots of memory available and processing power, but nevertheless I wanted to know some specific details :

  • XPathDocument is a read-only document whereas the XmlDocument is a read/write document. Hence, is XPathDocument more lightweight than XmlDocument since it lacks the writing capabilities?
  • I know for sure that when you load an XML document with XmlDocument it loads the entire document in memory, that might be a problem if the document size is big. Does XPathDocument do the same thing? if so, how can I read a single node or a set of nodes from an XML document without loading the entire document in memory first? I know I can use XmlTextReader but that means I have to parse the entire document as I access the nodes sequentially. I want to be able to query XML documents with an XPath expression.

Anyway, if the size of the object graphs don't differ much (XmlDocument and XPathDocument) it won't hurt to just get the job done, but I want to implement the best possible solution here.

like image 681
Nexus Avatar asked Aug 21 '11 02:08

Nexus


People also ask

What's the difference between XMLDocument and XmlReader?

XmlDocument is very easy to use. Its only real drawback is that it loads the whole XML document into memory to process. Its seductively simple to use. XmlReader is a stream based reader so will keep your process memory utilization generally flatter but is more difficult to use.

What is XMLDocument?

An XML document is a basic unit of XML information composed of elements and other markup in an orderly package. An XML document can contains wide variety of data. For example, database of numbers, numbers representing molecular structure or a mathematical equation.


1 Answers

XPathDocument reads the complete document into memory as well, that is needed to support all the XPath axes like preceding-sibling, preceding, ancestor, parent, child, descendant, following-sibling, following.

If you want to do XPath or XQuery queries without loading the whole document into memory then you have to look into specialized XML databases or at least into SQL databases with an XML data type with XQuery support. MS SQL server for instance has an XML data type and supports some (in my view rather limited) version of XQuery on that.

like image 179
Martin Honnen Avatar answered Nov 02 '22 23:11

Martin Honnen