htmlagilitypack: Find second table within a div

c# html-agility-pack

Question

I'm trying to parse information from a div that has 3 tables within it. I can get information from the first one without problem.

Code so far as follow:

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 find the second table information with HAP?

I've read some about nextsibling function but I can't get it to work.

Accepted Answer

var secondTable = res.SelectSingleNode("//table[2]");

Popular Answer

You could try going directly for the <td> tags instead by changing your Xpath string.

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

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


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