删除HTML注释 [英] Removing HTML Comments

查看:739
本文介绍了删除HTML注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从HTML文件中删除评论?



他们只能占用一行,但我确定会遇到这样的情况:注释可以跨越多行:

 <! - 单行注释。 - > 

<! - Multi-
行评论。
Lots''``〜|} {@#$%^& *())的第556行
中的
this
comme-
nt! - >


解决方案

Html Agility Pack .NET库。以下是一篇介绍如何在SO上使用它的文章:如何使用HTML Agility包



这是C#删除注释的代码:

  HtmlDocument doc = new HtmlDocument(); 
doc.Load(yourFile.htm );

//使用XPATH获取所有注释节点
foreach(doc.DocumentNode.SelectNodes(// comment())中的HtmlNode注释)
{
comment.ParentNode.RemoveChild(comment);
}
doc.Save(Console.Out); //在控制台上显示doc w / o注释
/ pre>

How would one go about removing Comments from HTML files?

They may only take up a single line, however I'm sure I'll run into cases where a comment may span across multiple lines:

<!-- Single line comment. -->

<!-- Multi-
ple line comment.
Lots      '""' '  "  ` ~ |}{556             of      !@#$%^&*())        lines
in
this
comme-
nt! -->

解决方案

You could use the Html Agility Pack .NET library. Here is an article that explains how to use it on SO: How to use HTML Agility pack

This is the C# code to remove comments:

    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

这篇关于删除HTML注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆