如何删除的XmlElement的所有子节点,但保留所有的属性? [英] How to remove all child nodes of an XmlElement, but keep all attributes?

查看:728
本文介绍了如何删除的XmlElement的所有子节点,但保留所有的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

请注意,该<一href="http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.removeall.aspx">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.

这篇关于如何删除的XmlElement的所有子节点,但保留所有的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆