Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

xpath

Using C#

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

like image 405
mrbradleyt Avatar asked Oct 02 '08 14:10

mrbradleyt


People also ask

How do you remove a specific tag in XML?

forEach( x => x. parentNode. remove(x) );

How do I select a specific node in XML?

To find nodes in an XML file you can use XPath expressions. Method XmlNode. SelectNodes returns a list of nodes selected by the XPath string. Method XmlNode.

How to remove node in dom?

Removing Nodes from the DOM Child nodes can be removed from a parent with removeChild() , and a node itself can be removed with remove() .

What is/* in XPath?

/* selects the root element, regardless of name. ./* or * selects all child elements of the context node, regardless of name.


1 Answers

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);
like image 144
jocheng Avatar answered Oct 20 '22 12:10

jocheng