title: Insert sibling node into xml document
body: <p>I have a .net web application that uses XmlDocument to load an XML String.  The format of the XML is as follows:</p>
  
  <pre><code>&lt;Sections name="Section Opening Page" PageContentID="201" Template="ReportTags"&gt;
    &lt;Chapter Name="Introduction" PageContentID="202" Template="ReportTags"&gt;
      &lt;Pages Name="Why this Document?" PageContentID="203" Template="ReportTags" /&gt;
      &lt;Pages Name="Target Audience" PageContentID="204" Template="ReportTags" /&gt;
    &lt;/Chapter&gt;
    &lt;Chapter Name="Detailed Results" PageContentID="205" Template="ReportTags"&gt;
      &lt;Pages Name="Question List" PageContentID="206" Template="ReportTags" /&gt;
      &lt;Pages Name="Answers" PageContentID="207" Template="ReportTags" /&gt;
      &lt;Pages Name="Comments" PageContentID="208" Template="ReportTags" /&gt;
    &lt;/Chapter&gt;
    &lt;Chapter Name="Appendix 1" PageContentID="209" Template="ReportTags"&gt;
      &lt;Pages Name="Page 1" PageContentID="210" Template="ReportTags" /&gt;
      &lt;Pages Name="Page 2" PageContentID="211" Template="ReportTags" /&gt;
      &lt;Pages Name="Page 3" PageContentID="212" Template="ReportTags" /&gt;
    &lt;/Chapter&gt;
    &lt;Chapter Name="Appendix 2" PageContentID="213" Template="ReportTags"&gt;
      &lt;Pages Name="Page 1" PageContentID="214" Template="ReportTags" /&gt;
      &lt;Pages Name="Page 2" PageContentID="215" Template="ReportTags" /&gt;
    &lt;/Chapter&gt;
  &lt;/Sections&gt;
  </code></pre>
  
  <p>As an example, I am trying to insert a NEW NODE under the 'Chapter' named 'Detailed Results' BETWEEN 'Page' named 'Question List' and 'Answers'.  The code I am using to do this is as follows:</p>
  
  <pre><code>try
  {
  string sPath = "Sections/Chapter/Pages[@PageContentID='" + selectedNode.Value + "']";
  XmlNode xmlNode = xmlDoc.SelectSingleNode(sPath);
  
  XmlElement xNewChild = xmlDoc.CreateElement("Pages");
  xNewChild.SetAttribute("Name", "Another New Page");
  xNewChild.SetAttribute("PageContentID", Guid.NewGuid().ToString());
  xNewChild.SetAttribute("Template", "ReportTags");
  
  xmlDoc.DocumentElement.InsertAfter(xNewChild, xmlNode); 
  }
  catch (Exception ex)
  {
  System.Diagnostics.Debug.WriteLine(ex.Message);
  }
  </code></pre>
  
  <p>However, it is not working, my new node is not inserted into the proper place, it is inserted at the top.</p>
  
  <p>Any help would be greatly appreciated!!</p>