3 个答案:
答案 0 :(得分:14)
这是删除评论的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
答案 1 :(得分:4)
这个带有微小调整的功能应该有效: -
private string RemoveHTMLComments(string input)
{
string output = string.Empty;
string[] temp = System.Text.RegularExpressions.Regex.Split(input, ""))
{
str = s;
}
else
{
str = s.Substring(s.IndexOf("-->") + 3);
}
if (str.Trim() != string.Empty)
{
output = output + str.Trim();
}
}
return output;
}
不确定它是否是最佳解决方案......
答案 2 :(得分:3)
不是最好的解决方案,而是一个简单的通过算法。应该做的伎俩
List output = new List();
bool flag = true;
foreach ( string line in System.IO.File.ReadAllLines( "MyFile.html" )) {
int index = line.IndexOf( "" )) {
output.Add( line.Substring( line.IndexOf( "-->" ) + 3 ));
flag = true;
}
}
System.IO.File.WriteAllLines( "MyOutput.html", output );