I have a .net web application that uses XmlDocument to load an XML String. The format of the XML is as follows:
<Sections name="Section Opening Page" PageContentID="201" Template="ReportTags">
<Chapter Name="Introduction" PageContentID="202" Template="ReportTags">
<Pages Name="Why this Document?" PageContentID="203" Template="ReportTags" />
<Pages Name="Target Audience" PageContentID="204" Template="ReportTags" />
</Chapter>
<Chapter Name="Detailed Results" PageContentID="205" Template="ReportTags">
<Pages Name="Question List" PageContentID="206" Template="ReportTags" />
<Pages Name="Answers" PageContentID="207" Template="ReportTags" />
<Pages Name="Comments" PageContentID="208" Template="ReportTags" />
</Chapter>
<Chapter Name="Appendix 1" PageContentID="209" Template="ReportTags">
<Pages Name="Page 1" PageContentID="210" Template="ReportTags" />
<Pages Name="Page 2" PageContentID="211" Template="ReportTags" />
<Pages Name="Page 3" PageContentID="212" Template="ReportTags" />
</Chapter>
<Chapter Name="Appendix 2" PageContentID="213" Template="ReportTags">
<Pages Name="Page 1" PageContentID="214" Template="ReportTags" />
<Pages Name="Page 2" PageContentID="215" Template="ReportTags" />
</Chapter>
</Sections>
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:
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);
}
However, it is not working, my new node is not inserted into the proper place, it is inserted at the top.
Any help would be greatly appreciated!!