HtmlAgilityPack - 如何获取单引号中包含的属性值?

HtmlAgilityPack - how to get attribute value covered in single quote?

我有以下 HTML:

<div id="search_posts">
<article class="xxx"  data-id='79642521778' data-type='photoset' <!-- many other attributes in single quotes-->> Article text 1 </article>
<article class="xxx"  data-id='84701653287' data-type='photoset' <!-- many other attributes in single quotes-->> Article text 2 </article>
</div>

我需要获取第 1 篇文章的 id,即 79642521778。

到目前为止我做了什么:

//assuming that the HtmlDocument has already loaded
string test = doc.GetElementbyId("search_posts").SelectNodes("//article").First().OuterHtml;
test = doc.DocumentNode.GetAttributeValue("data-id", "NULL");

它 returns 为空。我怎样才能得到正确的价值?谢谢

您正在尝试获取 doc.DocumentNode 上的 data-id 属性,而不是 article 节点。

var articles = doc.GetElementbyId("search_posts").SelectNodes("//article");
var firstDataId = articles.First().GetAttributeValue("data-id", "NULL");