Replacing the XmlNodes within an XmlDocument

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.

Listing 11.25.
 C# XmlDocument doc = new XmlDocument(); doc.LoadXml("<root att1=\"first\" att4=\"last\"/>"); XmlElement newRoot = doc.CreateElement("NewRoot"); ...

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.