Dmitrij Kultasev
Dmitrij Kultasev

Reputation: 5787

How to get URL from the XPATH?

I've tried to check other answers on this site, but none of them worked for me. I have following HTML code:

<h3 class="x-large lheight20 margintop5">
  <a href="http://someUrl.com" class="marginright5 link linkWithHash detailsLink"><strong>some textstring</strong></a>
</h3>

I am trying to get # from this document with following code:

string adUrl = Doc.DocumentNode.SelectSingleNode("//*[@id=\"offers_table\"]/tbody/tr["+i+ "]/td/table/tbody/tr[1]/td[2]/div/h3/a/@href").InnerText;

I've also tried to do that without @href. Also tried with a[contains(@href, 'searchString')]. But all of these lines gave me just the name of the link - some textstring

Upvotes: 0

Views: 1064

Answers (2)

wingerse
wingerse

Reputation: 3796

Attributes doesn't have InnerText.You have to use the Attributes collection instead.

string adUrl = Doc.DocumentNode.SelectSingleNode("//*[@id=\"offers_table\"]/tbody/tr["+i+ "]/td/table/tbody/tr[1]/td[2]/div/h3/a")
                               .Attributes["href"].Value;

Upvotes: 3

CodingMadeEasy
CodingMadeEasy

Reputation: 2337

Why not just use the XDocument class?

private string GetUrl(string filename)
{
    var doc = XDocument.Load(filename)
    foreach (var h3Element in doc.Elements("h3").Where(e => e.Attribute("class"))
    {
        var classAtt = h3Element.Attribute("class");
        if (classAtt == "x-large lheight20 margintop5")
        {
            h3Element.Element("a").Attribute("href").value;
        }
    }
}

The code is not tested so use with caution.

Upvotes: 1

Related Questions