title: Add a parent tag using XML linq
body: <p>Hello what I want is add another parent tag after the items tag</p>
  
  <p>Here is the example XML:</p>
  
  <pre><code>&lt;parent&gt;
    &lt;items&gt;
      &lt;book&gt;
        &lt;pen&gt;&lt;/pen&gt;
       &lt;/book&gt;
    &lt;/items&gt;
  &lt;/parent&gt;
  </code></pre>
  
  <p>and here is the desired output: </p>
  
  <pre><code>&lt;parent&gt;
    &lt;items&gt;
     &lt;paper&gt;
      &lt;book&gt;
        &lt;pen&gt;&lt;/pen&gt;
      &lt;/book&gt;
     &lt;/paper&gt;
    &lt;/items&gt;
  &lt;/parent&gt;
  </code></pre>
  
  <p>and here is my code.  What's wrong with this?</p>
  
  <pre><code>string xml = "&lt;parent&gt;&lt;items&gt;" +
                  "&lt;book&gt;" +
                  "&lt;pen&gt;" +
                  "&lt;/pen&gt;" +
                  "&lt;/book&gt;" +
                  "&lt;/items&gt;&lt;/parent&gt;";
  
  
  
  XDocument doc = XDocument.Parse(xml);
  Console.WriteLine(doc.ToString());
  var books = doc.Descendants("items").Elements().First();
  doc.Element("items").Add(new XElement("paper", books));
  books.Remove();
  
  Console.WriteLine(doc.ToString());
  </code></pre>
  
  <p>It only gives me error:  </p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>pointing to <code>doc.Element("items").Add(new XElement("paper", books));</code></p>
  
  <p>the answer below which i marked as checked worked in the example xml</p>
  
  <p>but when i used using the long xml it doesn't work anymore</p>
  
  <p>here is the link of the long xml</p>
  
  <p><a href="http://pastebin.com/nb6Pk8bX" rel="nofollow noreferrer">http://pastebin.com/nb6Pk8bX</a></p>
  
  <p>and here is the output</p>
  
  <p><a href="http://pastebin.com/WkZEgUYy" rel="nofollow noreferrer">http://pastebin.com/WkZEgUYy</a></p>
  
  <p>and here is the code im using</p>
  
  <pre><code> XDocument doc = XDocument.Parse(reader);
                  var books = doc.Descendants("Layout").Elements().First();
                  books.Ancestors().First().Add(new XElement("Page", books));
                  books.Remove();
  </code></pre>
  
  <p>the <code>page</code> tag became the parent. not the <code>layout</code> tag</p>
  
  <pre><code>document&gt;
    &lt;Page&gt;
      &lt;Layout&gt;
  </code></pre>