Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How big is the speed difference between XPathNavigator and XmlReader, really?

Tags:

I've got a fairly big XML file that I need to parse into a .NET class structure (to be mapped to a fixed-length record format and transmitted via MQ). Performance is important, but not absolutely critical.

I almost always use XPathNavigator to read XML files because it's much easier than XmlReader. On the other hand, I know XmlReader is faster than XPathNavigator, because it theoretically just reads one node at a time whereas XPathNavigator has to read enough to execute an XPath, possibly the whole document.

My question is: how much faster is it really? Will it make a noticeable difference when reading a few thousand nodes? What's the tipping point where I pretty much have to switch to XmlReader? Or is XPathNavigator optimized to the point that it's always a good option?

Most of my XML experience is on relatively small files, so I'm looking for input from anybody who's worked with big files.

like image 562
John M Gant Avatar asked Aug 28 '09 20:08

John M Gant


1 Answers

As far as I'm aware the only places where you can get an implementation of XPathNavigator is from either the .Xml.Linq.XDocument or the XPathDocument both of which hold the entire tree in memory.

The XmlReader on the other hand can retrieve and parse a stream of XML without the need to collect a set of nodes into a tree.

Hence assuming you gather all your required data in a forward only manner, then for large data sets XmlReader should out perform XPathNavigator simply on the basis that:-

  1. You would have had to deserialise a stream of XML anyway to populate a document and
  2. You would not have to load a large set of items into memory.
like image 148
AnthonyWJones Avatar answered Oct 12 '22 07:10

AnthonyWJones