0
Posted on 2:51 AM by Softminer and filed under

Fixing nested HtmlAgility SelectSingleNode

var firstDiv = page.DocumentNode.SelectSingleNode("//div");
//Returns the first div on the page, as it should. But you cannot dig into this.
 
var firstDivTitle = firstDiv.SelectSingleNode("//h2");
// This returns the first <h2> on the entire page, not the one in the div.

To fix it you have to write 

var firstDivTitle = firstDiv.SelectSingleNode(".//h2");

another solution is to mix the tags 


var firstDivTitle = page.DocumentNode.SelectSingleNode("//div//h2");
0
Responses to ... Fixing nested Html Agility SelectSingleNode