Removing XmlNodes for an XmlDocument

In addition to inserting and replacing nodes, you may need to remove a node from the tree altogether. You can easily remove a node from the node tree by using the RemoveChild method on the XmlNode class.

The RemoveChild method takes one parameter: the node to remove. If this node is successfully removed, then it returns a reference to the removed node. If the node is not a child of the container node, then an ArgumentException is raised. Listing 11.26 shows how to use the RemoveChild method.

Listing 11.26.
 C# XmlDocument doc = new XmlDocument(); doc.LoadXml("<root><child/></root>"); doc.DocumentElement.RemoveChild(doc.DocumentElement.FirstChild); doc.Save("out.xml"); VB Dim doc As New XmlDocument() doc.LoadXml("<root><child/></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.