2017-05-12 64 views

回答

2

您可以通過使用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; 
    ... 
} 
相關問題