0

I have a question about Regex pattern for removing comments from XML document. I have two kinds of comments:

enter image description here

single line

enter image description here

and multiline.

For removing comments I used next code:

processedContent = File.ReadAllText(xml);
Regex regex = new Regex(@"<!--(\n|.)*-->\r\n");
processedContent = regex.Replace(processedContent, "");

It works fine for the multiline XML comment but for the single line comment all nodes between comments are removed also and I have such an output for the first sample:

enter image description here

Any help about correct "universal" Regex pattern for such cases?

Bill
  • 3,391
  • 3
  • 13
  • 17
  • Why on earth would you want to use a regex for this? Use an XmlReader and XmlWriter, an XDocument or an XSLT Transformation. That takes care of all the other possible (corner) cases your regex will break but didn't yet find. – rene Apr 03 '17 at 10:23
  • Don't use regex for this! Use LinqToXml: `var xml = XElement.Load("in.xml"); xml.DescendantNodes().OfType().ToList().Remove(); xml.Save("out.xml");` – Alexander Petrov Apr 03 '17 at 10:30

0 Answers0