title: Is there a possibility for cloning an xmlTextReader (or multi-pass reading)?
body: <p>I currently have to fix an existing application to use something other than the <a href="http://xmlsoft.org/html/libxml-tree.html" rel="nofollow">DOM interface</a> of <a href="http://www.xmlsoft.org/index.html" rel="nofollow">libxml2</a> because it turns out it gets passed XML files so large that they can't be loaded into memory.</p>
  
  <p>I have rewritten the data loading from iterating over the DOM tree to using <a href="http://www.xmlsoft.org/html/libxml-xmlreader.html" rel="nofollow">xmlTextReader</a> for the most part now without too much problems. ( I use <code>xmlNewTextReaderFilename</code> to open a local file.)</p>
  
  <p>It turns out however, that the subtree where the large data resides has to be read not in-order, but I have to collect some (small amount of) data before the other. (And the problem is exactly that it is this subtree that contains the large volume of data, so loading only this subtree into memory doesn't make much sense either.)</p>
  
  <p>The easiest thing would be to just "clone" / "copy" my current reader, read ahead and then return to the original instance to continue reading there. (Seems I'm <a href="http://p2p.wrox.com/xml/40493-cloning-xmltextreader.html" rel="nofollow">not the first one</a> ... There's even something implemented on the C# side: <a href="http://msdn.microsoft.com/en-us/library/aa302292.aspx" rel="nofollow">XML Reader with Bookmarks</a>.)</p>
  
  <p>There doesn't appear to be any way however to "copy" the state of an xmlTextReader.</p>
  
  <p>If I can't re-read <em>part</em> of a file, I could also re-read the <em>whole</em> file, which, although wasteful, would be OK here, but I still would need to remember where I was beforehand?</p>
  
  <p>Is there maybe a simple way to remember for a xmlTextReader where it is in the current document, so that I can later find that position again when reading the document/file a second time?</p>
  
  <p>Here's a problem example:</p>
  
  <pre><code>&lt;root&gt;
    &lt;cat1&gt;
      &lt;data attrib="x1"&gt;
        ... here goes up to one GB in stuff ...
      &lt;/data&gt;
      &lt;data attrib="y2"&gt; &lt;!-- &lt;&lt;&lt; Want to remember this position without having to re-read the stuff before --&gt;
        ... even more stuff ...
      &lt;/data&gt;
      &lt;data attrib="z3"&gt;
         &lt;!-- I need (part of) the data here to meaningfully interpret the data in [y2] that 
              came before. The best approach would seem to first skip all that data
              and then start back there at &lt;data attrib="y2"&gt; ... not having to re-read
              the whole [x1] data would be a big plus! --&gt;
      &lt;/data&gt;
    &lt;/cat1&gt;
    ...
  &lt;/root&gt;
  </code></pre>