0

I want to get the href-link from this:

<a class="abc" href="/subsite/2014/05/19/site.html"> <p>test1</p><p>test2</p> </a>

I'm trying this:

var nodes = doc.DocumentNode.SelectNodes("//a[@class='abc']/@href");

...InnerHtml becomes <p>test1</p><p>test2</p>, not the link in the href...

mdc
  • 1,161
  • 6
  • 22
  • 37
  • What do you mean by "getting the elements of abc"? – Wayne May 19 '14 at 16:58
  • 2
    The XPath expression is correct, it would actually select `href` attributes. However: [Html Agility Pack does not support attribute selection.](http://stackoverflow.com/questions/541953/selecting-attribute-values-with-html-agility-pack) Iterate the elements you get to extract the individual attribute values. – Tomalak May 19 '14 at 16:59
  • @Tomalak The link is not inlcuded in the results. Its in the OuterHtml but I must get closer to the actual URL than that before I iterate. – mdc May 19 '14 at 17:10
  • Can you do something like `doc.DocumentNode.SelectNode("//a[@class='abc']").getAttribute("href")`? – SiKing May 19 '14 at 19:06

2 Answers2

0

As explained by HtmlAgilityPack creator, @SimonMourier, here, you can't use SelectNodes() directly to get attributes (as the method name and the return type implies, it meant to select nodes).

You need to do it with different approach. Try to select the node instead of the attribute, then you can use LINQ extension method to extract attribute of each selected nodes, for example :

var attrs = doc.DocumentNode
               .SelectNodes("//a[@class='abc' and @href]")
               .Select(o => o.Attributes["href"]);
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
0

This will work too, as described in the link from @Tomalak.

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

        //Get value from given xpath
        string xpath = "//a[@class='abc']/@href";
        var val = navigator.Select(xpath);
mdc
  • 1,161
  • 6
  • 22
  • 37