ASP.net / HTMLAgilityPack : Getting Value of span within div

asp.net html-agility-pack

Question

Dim myHtml As New HtmlWeb
    Dim myPage As HtmlDocument = myHtml.Load("http://www.mysite.com")
    Dim myNode As HtmlAgilityPack.HtmlNode

    myNode = myPage.DocumentNode.SelectSingleNode("//div[@id='olpDivId']")

There are spans with the same classes inside of olpDivid.

<span class="blah><a href="fsdfs1>1</a></span>
<span class="blah><a href="fsdfs1>2</a></span>
<span class="blah><a href="fsdfs1>3</a></span>

The issue is that there are three spans with the same classes inside of my olpDivId, and I need to obtain the text from inside the second one.

1
1
6/1/2012 2:46:40 AM

Accepted Answer

I believe you are seeking this.

var myNode = myPage.DocumentNode.SelectSingleNode("//div[@id='olpDivId']/span[2]/a");

if (myNode != null)
{
    string value = myNode.InnerText.Trim();
}

Using the HTML snippet below, I tried this code.

<div id="olpDivId">
    <span class="blah"><a href="fsdfs1">1</a></span> <span class="blah"><a href="fsdfs1">
        2</a></span> <span class="blah"><a href="fsdfs1">3</a></span>
</div>
1
6/1/2012 9:58:12 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow