Html Agility Pack, SelectNodes from a node

thatsIT picture thatsIT · May 14, 2012 · Viewed 37.6k times · Source

Why does this pick all of my <li> elements in my document?

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

var travelList = new List<Page>();
var liOfTravels = doc.DocumentNode.SelectSingleNode("//div[@id='myTrips']")
                     .SelectNodes("//li");

What I want is to get all <li> elements in the <div> with an id of "myTrips".

Answer

ChristiaanV picture ChristiaanV · May 14, 2012

It's a bit confusing because you're expecting that it would do a selectNodes on only the div with id "myTrips", however if you do another SelectNodes("//li") it will performn another search from the top of the document.

I fixed this by combining the statement into one, but that would only work on a webpage where you have only one div with an id "mytrips". The query would look like this:

doc.DocumentNode.SelectNodes("//div[@id='myTrips'] //li");