C# HtmlAgilityPack-如何获得单引号中包含的属性值?
我有以下HTML:C# HtmlAgilityPack-如何获得单引号中包含的属性值?,c#,html-agility-pack,C#,Html Agility Pack,我有以下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-t
<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>
它返回NULL。如何获得正确的值?谢谢。您正在尝试获取
doc.document节点
上的数据id
属性,而不是您的文章
节点
var articles = doc.GetElementbyId("search_posts").SelectNodes("//article");
var firstDataId = articles.First().GetAttributeValue("data-id", "NULL");
不确定,HtmlAgilityPack仍然是最好的工具。看见考虑一下,哦,我忘了在我的代码中添加1行代码。但我已经根据你的回答重写了。谢谢
var articles = doc.GetElementbyId("search_posts").SelectNodes("//article");
var firstDataId = articles.First().GetAttributeValue("data-id", "NULL");