Opzione 1 Uso elem.InnerXml = "";
Il codice di lavoro pieno, se avete bisogno di questo:
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();
Detailied informazioni che cosa InnerXml fare:
public override string InnerXml
{
get
{
return base.InnerXml;
}
set
{
this.RemoveAllChildren();
new XmlLoader().LoadInnerXmlElement(this, value);
}
}
Ci potrebbe essere problema di prestazioni sul LoadInnerXmlElement ma perché noi avere una stringa vuota non dovrebbe essere grande, perché la maggior parte del tempo impiegherà questo metodo:
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;
}
Opzione 2 seguente codice:
XmlNode todelete = elem.FirstChild;
while (todelete != null)
{
elem.RemoveChild(elem.FirstChild);
todelete = elem.FirstChild;
}
Chi performane. Vediamo in XmlElement.RemoveAll() è:
public override void RemoveAll()
{
base.RemoveAll();
this.RemoveAllAttributes();
}
Dove base.RemoveAll() è esattamente:
[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);
}
}
Quindi è lo stesso come ho scritto sopra Sarebbe questa soluzione non
Questa è la soluzione che stavo aspettando! Ho verificato con Reflector che questa proprietà fa esattamente ciò che è richiesto. – usr