I'm optimizing a custom object -> XML serialization utility, and it's all done and working and that's not the issue.
It worked by loading a file into an XmlDocument object, then recursively going through all the child nodes.
I figured that perhaps using XmlReader instead of having XmlDocument loading/parsing the entire thing would be faster, so I implemented that version as well.
The algorithms are exactly the same, I use a wrapper class to abstract the functionality of dealing with an XmlNode vs. an XmlReader. For instance, the GetChildren methods yield returns either a child XmlNode or a SubTree XmlReader.
So I wrote a test driver to test both versions, and using a non-trivial data set (a 900kb xml file with around 1350 elements).
However, using JetBrains dotTRACE, I see that the XmlReader version is actually slower than the XmlDocument version! It seems that there is some significant processing involved in XmlReader.Read calls when i'm iterating over child nodes.
So I say all that to ask this:
What are the advantages/disadvantages of XmlDocument and XmlReader, and in what circumstances should you use either?
My guess is that there is a file size threshold at which XmlReader becomes more economical in performance, as well as less memory-intensive. However, that threshold seems to be above 1MB.
Response to Robert Rossney: I'm calling ReadSubTree every time to process child nodes:
public override IEnumerable<IXmlSourceProvider> GetChildren ()
{
XmlReader xr = myXmlSource.ReadSubtree ();
// skip past the current element
xr.Read ();
while (xr.Read ())
{
if (xr.NodeType != XmlNodeType.Element) continue;
yield return new XmlReaderXmlSourceProvider (xr);
}
}
Response to Aaron: That test applies to a lot of objects at a single level (ie wide & shallow) - but I wonder how well XmlReader fares when the XML is deep & wide? IE the XML i'm dealing with is much like a data object model, 1 parent object to Many child objects, etc: 1..M..M..M
I also don't know beforehand the structure of the XML i'm parsing, so I can't optimize for it.