【问题标题】:How to select node types which are HtmlNodeType.Comment using HTMLAgilityPack如何使用 HTMLAgilityPack 选择 HtmlNodeType.Comment 节点类型
【发布时间】:2011-04-18 14:51:08
【问题描述】:

我希望从 html 中删除类似的东西

<!--[if gte mso 9]>
...
<![endif]-->


<!--[if gte mso 10]>
...
<![endif]-->

如何在 C# 中使用 HTMLAgilityPack 做到这一点?

我正在使用

static void RemoveTag(HtmlNode node, string tag)
        {
            var nodeCollection = node.SelectNodes("//"+ tag );
            if(nodeCollection!=null)
                foreach (HtmlNode nodeTag in nodeCollection)
                {
                    nodeTag.Remove();
                }
        }

对于普通标签。

【问题讨论】:

  • 我什至不确定来自 microsoftword 的 sn-p 是 HtmlNodeType.Comment

标签: c# html-agility-pack


【解决方案1】:
        public static void RemoveComments(HtmlNode node)
        {
            foreach (var n in node.ChildNodes.ToArray())
                RemoveComments(n);
            if (node.NodeType == HtmlNodeType.Comment)
                node.Remove();
        }


        static void Main(string[] args)
        {
            var doc = new HtmlDocument();
            string html = @"<!--[if gte mso 9]>
...
<![endif]-->

<body>
    <span>
        <!-- comment -->
    </span>
    <!-- another comment -->
</body>

<!--[if gte mso 10]>
...
<![endif]-->";
            doc.LoadHtml(html);

            RemoveComments(doc.DocumentNode);
            Console.WriteLine(doc.DocumentNode.OuterHtml);
            Console.ReadLine();

        }

或者一个有趣的小 LINQ 风格:

public static IEnumerable<HtmlNode> Walk(HtmlNode node)
{
    yield return node;
    foreach (var child in node.ChildNodes)
        foreach (var x in Walk(child))
            yield return x;
}

...

foreach (var n in Walk(doc.DocumentNode).OfType<HtmlCommentNode>().ToArray())
    n.Remove();

更简单(忘了我们可以使用 xpath 来查找评论节点)

    var doc = new HtmlDocument();
    string html = @"
<!--[if gte mso 9]>
...
<![endif]-->

<body>
<span>
<!-- comment -->
</span>
<!-- another comment -->
</body>

<!--[if gte mso 10]>
...
<![endif]-->";
    doc.LoadHtml(html);
    foreach (var n in doc.DocumentNode.SelectNodes("//comment()") ?? new HtmlNodeCollection(doc.DocumentNode))
        n.Remove();

【讨论】:

  • +1 因为喜欢寻找更好的编码方法来完成相同的任务
【解决方案2】:

@Mark,合并了您的第三个示例来生成此示例,以供参考:

public static string CleanUpRteOutput(this string s)
        {
            if (s != null)
            {
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(s);
                RemoveTag(doc, "script");
                RemoveTag(doc, "link");
                RemoveTag(doc, "style");
                RemoveTag(doc, "meta");
                RemoveTag(doc, "comment");
...

和 removeTag 功能:

static void RemoveTag(HtmlAgilityPack.HtmlDocument doc, string tag)
        {
            foreach (var n in doc.DocumentNode.SelectNodes("//" + tag) ?? new HtmlAgilityPack.HtmlNodeCollection(doc.DocumentNode))
                n.Remove(); 
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    相关资源
    最近更新 更多