Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html Agility Pack, SelectNodes from a node

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".

like image 660
thatsIT Avatar asked May 14 '12 13:05

thatsIT


1 Answers

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");

like image 80
ChristiaanV Avatar answered Oct 13 '22 18:10

ChristiaanV