Using the insertion methods is not the only way to insert nodes into an XmlDocument. You can also insert new nodes by replacing nodes that already exist in the node tree by using the ReplaceChild method. The ReplaceChild method takes two parameters, much like the InsertBefore and InsertAfter methods, but instead of using the existing node as a reference point, the existing node is removed from the node tree, and the new node is inserted in its place. The method returns the node that has been replaced. Listing 11.25 demonstrates how to use the ReplaceChild method.
C# XmlDocument doc = new XmlDocument(); doc.LoadXml("<root att1=\"first\" att4=\"last\"/>"); XmlElement newRoot = doc.CreateElement("NewRoot"); ...