0

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

Dmitrij Kultasev
  • 5,447
  • 5
  • 44
  • 88
  • InnerText? Why are you trying to use it instead of getting attribute (which is what `href` is? Like http://stackoverflow.com/questions/3750678/getting-attribute-value-of-an-xml-document-using-c-sharp – Alexei Levenkov Nov 15 '15 at 20:52

2 Answers2

3

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;
wingerse
  • 3,670
  • 1
  • 29
  • 61
1

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.

CodingMadeEasy
  • 2,257
  • 4
  • 19
  • 31