title: How do I add an XElement to a document, avoiding the "incorrectly structured document" error?
body: <pre><code>        // Remove element with ID of 1
          var userIds = from user in document.Descendants("Id")
                         where user.Value == "1"
                         select user;
  
          userIds.Remove();
  
          SaveAndDisplay(document);
  
          // Add element back
          var newElement = new XElement("Id", "0", 
              new XElement("Balance", "3000"));
          document.Add(newElement);
  
          SaveAndDisplay(document);
  </code></pre>
  
  <p>The add element back block is the problem. As when it gets to the add it states:</p>
  
  <blockquote>
    <p>This operation would create an
    incorrectly structured document.</p>
  </blockquote>
  
  <p>What stupid mistake am I making?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Yes, I was reading as an <code>XDocument</code>, not <code>XElement</code>. Any advice on when to favour one or the other?</p>