Question

Using C#

How do you remove a specific node from an XMLDocument using XPATH?

Was it helpful?

Solution

XPath can only select nodes from a document, not modify the document.

OTHER TIPS

If you want to delete nodes, that are not direct children of the documents root, you can do this:

XmlDocument doc = new XmlDocument();
// ... fill or load the XML Document
XmlNode childNode = doc.SelectSingleNode("/rootnode/childnode/etc"); // apply your xpath here
childNode.ParentNode.RemoveChild(childNode);

Here you go. ChildNodeName, could be just the node name or an XPath query.

XmlDocument doc = new XmlDocument();

// Load you XML Document

XmlNode childNode = doc.SelectSingleNode(childNodeName);

// Remove from the document
doc.RemoveChild(childNode);

There is a different way using Linq, but I guessed you were using .NET 2.0

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top