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?
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.
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.
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