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);
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:
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:
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.
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.
You can get know the parent node:
node.ParentNode.RemoveChild(node);
Please note that the node.ParentNode
can be null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With