Here are some sample code for reading an XML file and return a string that is a file without comments.
var text = File.ReadAllText("c:\file.xml");
{
const string strRegex = @"<!--(?:[^-]|-(?!->))*-->";
const RegexOptions myRegexOptions = RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = text;
const string strReplace = @"";
string result = myRegex.Replace(strTargetString, strReplace);
return result;
}
Unfortunately, it just RegexOptions.Multilinewonβt perform the trick (which is a little contrary to intuition).
source
share