Working with the InsertBefore and InsertAfter Methods

If you need to insert a node in the middle of a container node's child list, you cannot use AppendChild or PrependChild. Instead, you must use InsertBefore and InsertAfter. Both methods take two XmlNode objects as parameters. The first XmlNode object is the XmlNode to be inserted. The second XmlNode object is the reference node. When using InsertBefore, the new node will be inserted before the reference node, and it will be inserted after the reference node when calling InsertAfter. Listing 11.22 creates two elements and adds one before the reference node and one after the reference node.

Listing 11.22.
 C# XmlDocument doc = new XmlDocument(); doc.LoadXml("<root><first/><last/></root>"); ...

Get Microsoft® .NET Compact Framework Kick Start now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.