title: Deciding on when to use XmlDocument vs XmlReader
body: <p>I'm optimizing a custom object -> XML serialization utility, and it's all done and working and that's not the issue.</p>
  
  <p>It worked by loading a file into an <code>XmlDocument</code> object, then recursively going through all the child nodes.</p>
  
  <p>I figured that perhaps using <code>XmlReader</code> instead of having <code>XmlDocument</code> loading/parsing the entire thing would be faster, so I implemented that version as well.</p>
  
  <p>The algorithms are exactly the same, I use a wrapper class to abstract the functionality of dealing with an <code>XmlNode</code> vs. an <code>XmlReader</code>.   For instance, the <code>GetChildren</code> methods yield returns either a child <code>XmlNode</code> or a SubTree <code>XmlReader</code>.</p>
  
  <p>So I wrote a test driver to test both versions, and using a non-trivial data set (a 900kb XML file with around 1,350 elements).</p>
  
  <p>However, using JetBrains dotTRACE, I see that the <code>XmlReader</code> version is actually slower than the <code>XmlDocument</code> version!    It seems that there is some significant processing involved in <code>XmlReader</code> read calls when I'm iterating over child nodes.</p>
  
  <p>So I say all that to ask this:</p>
  
  <p><strong>What are the advantages/disadvantages of <code>XmlDocument</code> and <code>XmlReader</code>, and in what circumstances should you use either?</strong></p>
  
  <p>My guess is that there is a file size threshold at which <code>XmlReader</code> becomes more economical in performance, as well as less memory-intensive.   However, that threshold seems to be above 1MB.</p>
  
  <p>I'm calling <code>ReadSubTree</code> every time to process child nodes:</p>
  
  <pre><code>public override IEnumerable&lt;IXmlSourceProvider&gt; 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);
      }
  }
  </code></pre>
  
  <p>That test applies to a lot of objects at a single level (i.e. wide &amp; shallow) - but I wonder how well <code>XmlReader</code> fares when the XML is deep &amp; wide?   I.e. the XML I'm dealing with is much like a data object model, 1 parent object to many child objects, etc: <code>1..M..M..M</code></p>
  
  <p>I also don't know beforehand the structure of the XML I'm parsing, so I can't optimize for it.</p>