Thursday, October 15, 2009

[.NET] XmlDocument VS XmlReader

To cut a long story short I recently learned (thanks to a co-worker) that XmlDocument performance sucks.

It's very handy to manipulate XML stuff with XmlDocument because it comes with all the DOM stuff, and for example you can use XPath for node selection like this:
// load up the xml doc
XmlDocument doc = new XmlDocument();

// get a list of all the nodes you need with xPath
XmlNodeList nodeList = doc.SelectNodes("root/whatever/nestedWhatever");
The above is quite cool and it works just fine if you're not loading hundreds of files for a total size of hundreds of MBs, in which case you'll notice a lethal blow to performance.

If you need speed you wanna go with XmlReader, a bare bones class that will scan the old way (forward only) element after element your XML file. Bad thing is that you won't have all the nice DOM stuff, so you'll have to manually parse elements retrieving innerText and/or attribute values. An example:
XmlReader xmlReader = XmlReader.Create(fileName);

while (xmlReader.Read())
//keep reading until we see my element
if (xmlReader.Name.Equals("myElementName") && (xmlReader.NodeType == XmlNodeType.Element))
// get attributes (or innerText) from the Xml element here
string whatever = xmlReader.GetAttribute("whatever");
// do stuff
I can't be bothered benchmarking as it bores the sh*t out of me - but performance increases a whole lot with XmlReader, and if you want figures to look at you can find plenty on google (this guy here did a pretty good job for example) and here's another good overview of XML classes (from Scott Hanselman's blog).

Anyway - here comes the common sense advice - whatever you're doing go with XmlDocument, if it's too slow for you needs switch over to XmlReader and you'll be grand.


Anonymous said...

As of 5 years ago: use XPathNavigator.

Johnny Idol said...


Thanks for pointing out - I am just out of cryonics (straight out of the late 90's) and I'm still catching up with *all this technology*

Never heard of XPathNavigator, also seem to understand that XPathReader is a good compromise combining XmlReader and XPath -->