如何使用HTMLAgilityPack選擇HtmlNodeType.Comment的節點類型

c# html-agility-pack

我想從html中刪除像

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


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

如何使用HTMLAgilityPack在C#中執行此操作?

我在用著

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


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

對於普通標籤。

一般承認的答案

        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 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();

        }

更容易(忘了我們可以用xpath來查找註釋節點)

        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();

        }

熱門答案

@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函數:

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");
...



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因