user1151923
user1151923

Reputation: 1872

Removing a child node from XmlNode

I am using XPath to select a report node. Now what I want to know here is how can I remove that node from the document without knowing which node's children they are?

I tried calling .RemoveChild and it throws this error :

The node to be removed is not a child of this node.

This is my code for deleting a node :

var node = doc.SelectSingleNode("//report");
doc.RemoveChild(node);

Upvotes: 5

Views: 3195

Answers (1)

Dmitry
Dmitry

Reputation: 14059

You can get know the parent node:

node.ParentNode.RemoveChild(node);

Please note that the node.ParentNode can be null.

Upvotes: 6

Related Questions