使用 html-agility-pack 查找所有具有数据属性的元素
find all elements with data - attribute using html-agility-pack
我正在使用 Html-agility-Pack 来解析 Html 文本块。是否可以通过属性/属性值找到所有元素的列表?
例如,下面是示例 html 文本。使用 Html-agility-pack 如何找到所有具有 "data-glossaryid" 属性的元素?
<p> sample text <a href="" data-glossaryid="F776EB48BD"></a>
<p><img alt="my pic" src="/~/media/Images/mypic.jpg" /></p>
sample text
<a href="" data-glossaryid="5D476EB49E"></a>
<p> more sample text </p>
<span data-glossaryid="F776EB49EF"> </span>
// the html block of text to parse
var a = @"<p> sample text <a href="""" data-glossaryid=""F776EB48BD""></a>
<p><img alt=""my pic"" src=""/~/media/Images/mypic.jpg"" /></p>
sample text <a href="""" data-glossaryid=""5D476EB49E""></a>
<p> more sample text </p>
<span data-glossaryid=""F776EB49EF""> </span>";
// create an HtmlDocument
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(a);
// get all elements with the attr data-glossaryid and prints its values
foreach (var item in htmlDocument.DocumentNode.SelectNodes("//*[@data-glossaryid]"))
Console.WriteLine(item.GetAttributeValue("data-glossaryid", ""));
我正在使用 Html-agility-Pack 来解析 Html 文本块。是否可以通过属性/属性值找到所有元素的列表?
例如,下面是示例 html 文本。使用 Html-agility-pack 如何找到所有具有 "data-glossaryid" 属性的元素?
<p> sample text <a href="" data-glossaryid="F776EB48BD"></a>
<p><img alt="my pic" src="/~/media/Images/mypic.jpg" /></p>
sample text
<a href="" data-glossaryid="5D476EB49E"></a>
<p> more sample text </p>
<span data-glossaryid="F776EB49EF"> </span>
// the html block of text to parse
var a = @"<p> sample text <a href="""" data-glossaryid=""F776EB48BD""></a>
<p><img alt=""my pic"" src=""/~/media/Images/mypic.jpg"" /></p>
sample text <a href="""" data-glossaryid=""5D476EB49E""></a>
<p> more sample text </p>
<span data-glossaryid=""F776EB49EF""> </span>";
// create an HtmlDocument
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(a);
// get all elements with the attr data-glossaryid and prints its values
foreach (var item in htmlDocument.DocumentNode.SelectNodes("//*[@data-glossaryid]"))
Console.WriteLine(item.GetAttributeValue("data-glossaryid", ""));