Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all child nodes of an XmlElement, but keep all attributes?

How to remove all child nodes of an XmlElement, but keep all attributes?

Note, that XmlElement.RemoveAll also removes all attributes. What is a clean, elegant and well-performing way to remove all child nodes? In other words, what is best-practice here?

like image 428
usr Avatar asked Apr 10 '13 12:04

usr


People also ask

How to delete node in Xml?

To remove a node from the XML Document Object Model (DOM), use the RemoveChild method to remove a specific node. When you remove a node, the method removes the subtree belonging to the node being removed; that is, if it is not a leaf node.


1 Answers

For a truly efficient solution:

e.IsEmpty = true;

is your fastest and simplest option. It does exactly what you requested: all inner text and nested elements are discarded, while attributes are retained.

like image 190
Parsley Avatar answered Sep 21 '22 06:09

Parsley