如何删除的XmlElement的所有子节点,但保留所有的属性?有的、节点、属性、XmlElement

2023-09-04 00:54:15 作者:皆是孤独

如何删除的所有子节点的XmlElement ,但保留所有属性?

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

请注意,该XmlElement.RemoveAll还会删除所有属性。什么是一个干净,优雅,性能良好的方法,以消除所有子节点?换句话说,什么是最好的做法在这里?

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?

推荐答案

对于一个真正有效的解决方案:

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.