noxious
noxious

Reputation: 67

htmlagilitypack: Find second table within a div

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.

Upvotes: 1

Views: 6327

Answers (3)

Ichabod Clay
Ichabod Clay

Reputation: 2011

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);
}

Upvotes: 0

Bert
Bert

Reputation: 82489

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

Upvotes: 7

HatSoft
HatSoft

Reputation: 11201

You can iterate through the collection of table inside the div this way

foreach(HtmlNode table in doc.res.SelectNodes("//table"])
{
  if(table != null)
  {
    var charName = table.InnerText.Substring(5).Trim();
  }
}

Upvotes: 0

Related Questions