Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all comment tags from XmlDocument

Tags:

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);     } 
like image 456
Filburt Avatar asked Dec 09 '09 14:12

Filburt


People also ask

What is XmlDocument?

The XmlDocument class is an in-memory representation of an XML document. It implements the W3C XML Document Object Model (DOM) Level 1 Core and the Core DOM Level 2. DOM stands for document object model. To read more about it, see XML Document Object Model (DOM).


1 Answers

When you load the xml, you can use XmlReaderSettings

XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; XmlReader reader = XmlReader.Create("...", settings); xmlDoc.Load(reader); 

On an existing instance, your solution looks good.

like image 162
Guillaume Avatar answered Sep 17 '22 22:09

Guillaume