How would i go about to remove all comment tags from a XmlDocument instance?
Is there a better way than retrieving a XmlNodeList and iterate over those?
XmlNodeList list = xmlDoc.SelectNodes("//comment()");
foreach(XmlNode node in list)
{
node.ParentNode.RemoveChild(node);
}
When you load the xml, you can use XmlReaderSettings
On an existing instance, your solution looks good.
Nope thats about it, although I'd be inclind to place the nodes in a List first.
I'm not sure about the .NET implementation of
XmlNodeList
but I know that previous MSXML implementations loaded the list in lazy manner and code such as the above in the past would end up failing in some way as result of the DOM tree being modified as the List is enumerated.Today looking for the way how to extract
<!-- -->
from Visual Basic for Applications (not C#), I have found also nodeTypeString property, but it takes more space. Here is an example in VBA:It omitts document top parent comment nodes, but they can be retrieved somehow directly if needed, for example using
With xmldoc.documentElement.childNodes
.