Jim
Jim

Reputation: 2828

HtmlAgility Xpath Issue

I am trying to parse with HTMLAgility the following :

<span class="button">
<a role="anotherbutton" href="/gofor/15555445554/be?ref=t">Me</a>
</span>

with something like this :

foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//span[@class = 'button']/a[@role = 'anotherbutton']"))
        {
            string att = link.Attributes["href"].Value;
            txt_htmlResults.Text += att.ToString() + "\n";      
        }

However I always get null exception... My intention is to get the 15555445554. Can someone assist. Thank you in advance

Upvotes: 0

Views: 64

Answers (1)

tedbatchlor
tedbatchlor

Reputation: 41

threw it in a text file on my c drive:

    HtmlDocument doc = new HtmlDocument();
    doc.Load("C:\\temp\\stackhtml.html");
    //string link = doc.DocumentNode.SelectSingleNode("//span[@class='button']//a").OuterHtml;
    string rawLink = doc.DocumentNode.SelectSingleNode("//span[@class='button']//a").GetAttributeValue("href", "unkown");
    Console.WriteLine("rawLink: " + rawLink);
    string cleanedLink = rawLink.Substring(rawLink.IndexOf("r/")+2,rawLink.IndexOf("/b")-rawLink.IndexOf("r/")-2);
    Console.WriteLine("cleanedLink: " + cleanedLink);
    Console.ReadLine();

result:

enter image description here

Upvotes: 2

Related Questions