O'Reilly logo

Microsoft® .NET Compact Framework Kick Start by Ronnie Yates, Erik Rubin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required