serhiy1994
serhiy1994

Reputation: 59

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

I have the following 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>

I need to get the id of the 1st article, i.e. 79642521778.

What I done so far:

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

And it returns NULL. How I can get the proper value? Thanks.

Upvotes: 0

Views: 471

Answers (1)

Neil
Neil

Reputation: 1633

You are trying to get the data-id attribute on your doc.DocumentNode, not your article node.

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

Upvotes: 1

Related Questions