Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a child node from XmlNode

Tags:

c#

xmldocument

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);
like image 803
user1151923 Avatar asked Apr 17 '15 12:04

user1151923


People also ask

How do I remove a node from an XML file?

The removeChild () method removes a specified node. When a node is removed, all its child nodes are also removed. This code will remove the first <book> element from the loaded xml:

How do I remove a node from the parent node?

Remove the element node by using the removeChild () method from the parent node Remove Myself - Remove the Current Node The removeChild () method is the only way to remove a specified node. When you have navigated to the node you want to remove, it is possible to remove that node using the parentNode property and the removeChild () method:

What is the use of removechild () method in JavaScript?

The removeChild () method removes a specified node. When a node is removed, all its child nodes are also removed. The removeChild () method is the only way to remove a specified node.

What happened to the name child2 nodes?

But in your desired output, you only have the name child. The child2 nodes appear to have been replaced by child nodes. Unfortunately, the XML you show isn't even valid XML.


1 Answers

You can get know the parent node:

node.ParentNode.RemoveChild(node);

Please note that the node.ParentNode can be null.

like image 62
Dmitry Avatar answered Sep 28 '22 06:09

Dmitry