title: Create an XDocument from specific elements of a larger XML using XmlReader
body: <p>I have a very large XML file (> 100MB) and I want to only look at a few, small elements within it via Linq to XML. </p>
  
  <p>I know there are many ways to do this, but I want to keep this in memory to use in different places throughout my program, so, what I'm looking to do is to traverse through this document once with an <code>XMLReader</code> and create my own <code>XDocument</code> variable with only those elements I'll need to look at, but I'm not sure how to do this correctly.</p>
  
  <p>Here's the basics of the code I have so far:</p>
  
  <pre><code>XDocument parsedXml = new XDocument();
  
  using (var rdr = new XmlTextReader(fileName))
  {
      rdr.MoveToContent();
      rdr.Read();
  
      while (rdr.NodeType == XmlNodeType.Element)
      {
          switch (rdr.Name)
          {
              case "Node1":
              case "Node2":
              case "Node3":
                  XElement newNode = XElement.Load(rdr.ReadSubtree());
                  parsedXml.Add(newNode);
                  rdr.Read();
                  break;
              default:
                  rdr.Skip();
                  break;
          }
      }
      rdr.Close();
  }
  </code></pre>
  
  <p>It works for the first element I add, but then I get an error - <code>This operation would create an incorrectly structured document</code> when I try and add any other nodes.</p>
  
  <p>What is the best way to accomplish this??</p>
  
  <p>Thanks!!</p>