Sim*_*ier 14
您可以使用Html Agility Pack .NET库.这篇文章解释了如何在SO上使用它:如何使用HTML Agility包
这是删除注释的C#代码:
HtmlDocument doc = new HtmlDocument();
doc.Load("yourFile.htm");
// get all comment nodes using XPATH
foreach (HtmlNode comment in doc.DocumentNode.SelectNodes("//comment()"))
{
comment.ParentNode.RemoveChild(comment);
}
doc.Save(Console.Out); // displays doc w/o comments on console
Run Code Online (Sandbox Code Playgroud)
不是最好的解决方案,而是一个简单的传递算法。应该能解决问题
List<string> output = new List<string>();
bool flag = true;
foreach ( string line in System.IO.File.ReadAllLines( "MyFile.html" )) {
int index = line.IndexOf( "<!--" );
if ( index > 0 ) {
output.Add( line.Substring( 0, index ));
flag = false;
}
if ( flag ) {
output.Add( line );
}
if ( line.Contains( "-->" )) {
output.Add( line.Substring( line.IndexOf( "-->" ) + 3 ));
flag = true;
}
}
System.IO.File.WriteAllLines( "MyOutput.html", output );
Run Code Online (Sandbox Code Playgroud)