您可以通过使用Descendents
扩展方法从AngleSharp.Extensions.ApiExtensions
检索注释标记。注释不是元素,因此您无法像通常那样查询它们,但此扩展方法允许您检索特定类型的节点。
IEnumerable<IComment> comments = document.Descendents<IComment>();
实施例:
using AngleSharp;
using AngleSharp.Parser.Html;
using AngleSharp.Dom; // For IComment
using AngleSharp.Extensions; // For Descendents
var parser = new HtmlParser();
var source = @"<!-- Single line comment. -->
<!-- Multi-
ple line comment.
Lots '""""' ' "" ` ~ |}{556 of [email protected]#$%^&*()) lines
in
this
comme -
nt!-->";
var document = parser.Parse(source);
// Get all comment nodes
IEnumerable<IComment> comments = document.Descendents<IComment>();
// Get the text in the comment nodes
foreach (IComment comment in comments)
{
var textValue = comment.TextContent;
...
}