c# htmlagilitypack parse first link from div with class?

c# html-agility-pack

Question

I am trying to parse the first link in the html code below /search?id=3

<div class="brs_col">
  <p>
    <a href="/search?id=3">
      <b>
        vastu shastra
      </b>
    </a>
  </p>
  <p>
    <a href="/search?id=1">
      <b>
        bygga
      </b>
      bastu
    </a>
  </p>
</div>

I've tried to select it with the following XPATH, but cant seem to get any of them to work:

//div[@class='brs_col']//p//a[@href]
//div[@class='brs_col']//p[0]//a[@href]
//div[@class='brs_col']//p//a[0][@href]

Any ideas?

Popular Answer

This if you sure that is the first url in the whole HTML document:

doc.DocumentNode.SelectSingleNode("//a").Attributes["href"].Value;

Or this if you sure that is the first ulr in the class brs_col

doc.DocumentNode.SelectSingleNode("//div[@class='brs_col']//a").Attributes["href"].Value;



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why