trenoncourt / HtmlAgilityPack.CssSelectors.NetCore

NetStandard version of HtmlAgilityPack.CssSelector

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jquery Comment Node

liukaigsx opened this issue · comments

String tmpComment = "<p><!-- comment -->hello</p>";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(tmpComment);
Console.WriteLine(HtmlDocument.QuerySelector("p").InnerText);//<!-- comment -->hello

InnerText contains comment node but how to filter this?
Thanks.

Hi ,
You can use HtmlNodeType.Comment From HtmlAgilityPack to escape comments
example:

<div id="with-comments">
  <p><!-- comment 1 -->Hello</p>
  <p> <!-- comment 2 -->World!</p>
</div>
var element = Doc.QuerySelector("#with-comments");
string tt = element.ChildNodes
    .Where(d => d.NodeType == HtmlNodeType.Element)
    .SelectMany(d => d.ChildNodes)
    .Where(d => d.NodeType != HtmlNodeType.Comment)
    .Aggregate("", (s, n) => s + n.InnerHtml);

Here tt is equal to "Hello World!".
I just added this case in my unit tests if you want to try.