htmlagilitypack: Find second table within a div

c# html-agility-pack

Question

I'm attempting to extract data from adiv It has three tables. I have no trouble getting information from the first one.

Here is the code as of now:

HtmlAgilityPack.HtmlWeb doc = new HtmlAgilityPack.HtmlWeb();
HtmlAgilityPack.HtmlDocument htmldocObject = doc.Load(URL);
var res = htmldocObject.DocumentNode.SelectSingleNode("//div[@class='BoxContent']");

var firstTable = res.SelectSingleNode("//table");
var charName = firstTable.ChildNodes[i++].InnerText.Substring(5).Trim();

<div class="BoxContent">
    <table>
        <tr bgcolor=#505050>
            <td colspan=2 class=white>
            <b>I'm getting this text</b>
            </td>
        </tr>
        <tr bgcolor=#F1E0C6>
            <td>I get this too</td>
            <td>I'm getting this as well</td>
        </tr>
    </table>
    <table>
        <tr>
            <td>Trying to retrieve this</td>
        </tr>
    </table>
</div>

How can I use HAP to locate the second table's information?

Although I've read a little about the nextsibling function, I can't get it to operate.

1
1
7/27/2012 2:18:00 PM

Accepted Answer

var secondTable = res.SelectSingleNode("//table[2]");
7
7/26/2012 6:41:08 PM

Popular Answer

You may try pursuing the<td> simply altering your Xpath string to use tags instead.

HtmlNodeCollection tdNodeCollection = htmldocObject
                                     .DocumentNode
                                     .SelectNodes("//div[@class = 'BoxContent']//td");

foreach (HtmlNode tdNode in tdNodeCollection)
{
     Console.WriteLine(tdNode.InnerText);
}


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