title: Add sibling after a specific XML node C#
body: <p>My xml document looks like this:</p>
  
  <pre><code>&lt;root&gt;
  &lt;parent1&gt;
      &lt;element1&gt;
      &lt;/element1&gt;
  &lt;/parent1&gt;
  &lt;parent1&gt;
      &lt;element1&gt;
      &lt;/element1&gt;
  &lt;/parent1&gt;
  &lt;parent1&gt;
      &lt;element1&gt;
      &lt;/element1&gt;
  &lt;/parent1&gt;
  &lt;parent2&gt;
      &lt;element1&gt;
      &lt;/element1&gt;
  &lt;/parent2&gt;
  &lt;parent2&gt;
      &lt;element1&gt;
      &lt;/element1&gt;
  &lt;/parent2&gt;
  &lt;parent3&gt;
      &lt;element2&gt;
      &lt;/element2&gt;
  &lt;/parent3&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>I would like to add a new parent1 node after the last parent1 node before the first parent2 node.</p>
  
  <p>Like this:</p>
  
  <pre><code>&lt;root&gt;
      &lt;parent1&gt;
          &lt;element1&gt;
          &lt;/element1&gt;
      &lt;/parent1&gt;
      &lt;parent1&gt;
          &lt;element1&gt;
          &lt;/element1&gt;
      &lt;/parent1&gt;
      &lt;parent1&gt;
          &lt;element1&gt;
          &lt;/element1&gt;
      &lt;/parent1&gt;
      &lt;parent1&gt;
          &lt;element1&gt;
          &lt;/element1&gt;
      &lt;/parent1&gt;
      &lt;parent2&gt;
          &lt;element1&gt;
          &lt;/element1&gt;
      &lt;/parent2&gt;
      &lt;parent2&gt;
          &lt;element1&gt;
          &lt;/element1&gt;
      &lt;/parent2&gt;
      &lt;parent3&gt;
          &lt;element2&gt;
          &lt;/element2&gt;
      &lt;/parent3&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>I tried with the XmlDocument SelectSingleNode and AppendChild methods but it only adds the new child after the last parent3 node.</p>
  
  <p>EDIT:</p>
  
  <p>I tried with this code:</p>
  
  <pre><code>string newNode = "&lt;parent1/&gt;&lt;element1&gt;1&lt;/element1&gt;&lt;/parent1/&gt;";
  
  XmlDocumentFragment xmlDocFrag = xmlDocument.CreateDocumentFragment();
  xmlDocFrag.InnerXml = newNode;
  
  
  XmlElement element = (XmlElement)xmlDocument.SelectSingleNode(@"//root/parent1[last()]");
  
  
  element.InsertAfter(xmlDocFrag, element); //--&gt; this throws an error
  element.AppendChild(xmlDocFrag); //--&gt; this works but it inserts the new node inside the last parent1 node
  </code></pre>