Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting attribute values with html Agility Pack

I'm trying to retrieve a specific image from a html document, using html agility pack and this xpath:

//div[@id='topslot']/a/img/@src

As far as I can see, it finds the src-attribute, but it returns the img-tag. Why is that?

I would expect the InnerHtml/InnerText or something to be set, but both are empty strings. OuterHtml is set to the complete img-tag.

Are there any documentation for Html Agility Pack?

like image 522
Vegar Avatar asked Feb 12 '09 15:02

Vegar


1 Answers

You can directly grab the attribute if you use the HtmlNavigator instead.

//Load document from some html string
HtmlDocument hdoc = new HtmlDocument();
hdoc.LoadHtml(htmlContent);

//Load navigator for current document
HtmlNodeNavigator navigator = (HtmlNodeNavigator)hdoc.CreateNavigator();

//Get value from given xpath
string xpath = "//div[@id='topslot']/a/img/@src";
string val = navigator.SelectSingleNode(xpath).Value;
like image 191
Pierluc SS Avatar answered Oct 21 '22 11:10

Pierluc SS