如何删除XmlElement
的所有子节点,但保留所有属性?
注意,XmlElement.RemoveAll也会删除所有属性。什么是干净,优雅和良好的删除所有子节点的方法?换句话说,这里最好的做法是什么?
答案 0 :(得分:29)
真正有效的解决方案:
e.IsEmpty = true;
是您最快捷,最简单的选择。它完全按照您的要求执行:丢弃所有内部文本和嵌套元素,同时保留属性。
答案 1 :(得分:6)
这个解决方案不会更简单吗?
while(e.FirstChild != null)
e.RemoveChild(e.FirstChild);
答案 2 :(得分:3)
选项1
如果您需要,请使用elem.InnerXml = "";
完整的工作代码:
var doc = new XmlDocument();
doc.LoadXml("<x a1='a' a2='b'><child1/><child2/></x>");
var elem = doc.DocumentElement;
Console.WriteLine(elem.OuterXml);
Console.WriteLine("HasAttributes " + elem.HasAttributes);
Console.WriteLine("HasChildNodes " + elem.HasChildNodes);
elem.InnerXml = "";
Console.WriteLine(elem.OuterXml);
Console.WriteLine("HasAttributes " + elem.HasAttributes);
Console.WriteLine("HasChildNodes " + elem.HasChildNodes);
Console.ReadLine();
详细信息InnerXml做什么:
public override string InnerXml
{
get
{
return base.InnerXml;
}
set
{
this.RemoveAllChildren();
new XmlLoader().LoadInnerXmlElement(this, value);
}
}
LoadInnerXmlElement可能存在性能问题,但因为我们有空字符串,所以它不应该很大,因为大部分时间都采用这种方法:
internal XmlNamespaceManager ParsePartialContent(XmlNode parentNode, string innerxmltext, XmlNodeType nt)
{
this.doc = parentNode.OwnerDocument;
XmlParserContext context = this.GetContext(parentNode);
this.reader = this.CreateInnerXmlReader(innerxmltext, nt, context, this.doc);
try
{
this.preserveWhitespace = true;
bool isLoading = this.doc.IsLoading;
this.doc.IsLoading = true;
if (nt == XmlNodeType.Entity)
{
XmlNode newChild;
while (this.reader.Read() && (newChild = this.LoadNodeDirect()) != null)
parentNode.AppendChildForLoad(newChild, this.doc);
}
else
{
XmlNode newChild;
while (this.reader.Read() && (newChild = this.LoadNode(true)) != null)
parentNode.AppendChildForLoad(newChild, this.doc);
}
this.doc.IsLoading = isLoading;
}
finally
{
this.reader.Close();
}
return context.NamespaceManager;
}
选项2 以下代码:
XmlNode todelete = elem.FirstChild;
while (todelete != null)
{
elem.RemoveChild(elem.FirstChild);
todelete = elem.FirstChild;
}
关于performane。让我们看看XmlElement.RemoveAll()它是:
public override void RemoveAll()
{
base.RemoveAll();
this.RemoveAllAttributes();
}
其中base.RemoveAll()完全是:
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public virtual void RemoveAll()
{
XmlNode oldChild = this.FirstChild;
for (; oldChild != null; {
XmlNode nextSibling;
oldChild = nextSibling;
}
)
{
nextSibling = oldChild.NextSibling;
this.RemoveChild(oldChild);
}
}
所以它和我上面写的一样