Html Agility Pack C#: Expression must evaluate to a node-set

c# html-agility-pack web-scraping

Question

I'm using Html Agility Pack to fetch a webpage. I want to collect all the TEXT I AM LOOKING FOR of the following form:

<li><a href="/deal/map/4087664" class="show-location" title="bla bla" data-address="TEXT I AM LOOKING FOR"></a></li>

I tried this code:

var web = new HtmlWeb();
var doc = web.Load(url);

var nodes1 = doc.DocumentNode.SelectNodes("//[@data-address]");
var nodes2 = doc.DocumentNode.SelectNodes("//[@data-address={0}]");

both threw an exception: Expression must evaluate to a node-set. How can i correct my selector ?

Accepted Answer

I'm not an XPath expert by any means, but I suspect you want:

// Note the *
var nodes1 = doc.DocumentNode.SelectNodes("//*[@data-address]");

In other words "any element with a data-address attribute"




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why