views:

36

answers:

2

I am trying to get all links of a link when its parent class is name_of_box. I wrote the below but got nothing. How do i do this? With css i believe i can select it with .name_of_box a

var ls = htmldoc.DocumentNode.Elements("//div[@class='name_of_box']//a[@href]");
A: 

HtmlAgilityPack doesn't have the ability to directly query an attribute value. You have to loop over the list of anchor nodes. Here's one way:

var ls = new List<string>(); 
var nodes = htmldoc.DocumentNode.SelectNodes("//div[@class='name_of_box']//a");
nodes.ToList().ForEach(a => ls.Add(a.GetAttributeValue("href", "")));

But there is an experimental build you can look at, that will let you directly query an attribute.

Rohit Agarwal
A: 

Thanks for the article. Kickback

Britney