Yusril Maulidan Raji Yusril Maulidan Raji - 2 months ago 9
HTML Question

How to select td inside tfoot with HtmlAgilityPack?

I have this html:

<table>
<thead>
<tr>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Body1</td>
</tr>
<tr>
<td>Body2</td>
</tr>
<tr>
<td>Body3</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Footer</td>
</tr>
</tfoot>
</table>


I tried to get the
Inner.Html
of the
<td></td>
within
<tfoot></tfoot>
, but I have problem with it.
Here is what I have tried:

var mainGrid = htmlDoc.DocumentNode.SelectSingleNode("//table");
var footerGrid = mainGrid.SelectSingleNode("//tfoot");
var tdFooterGrid = footerGrid.SelectSingleNode("//td");
if (tdFooterGrid != null)
{
var a = tdFooterGrid.InnerHtml;
}


That was fine until I retrieved the
footerGrid
. When I continued to get the
td
in order to get the
Footer
text, it resulted the
Body 1
instead.

Is there any suggestion for this?

Answer

Below Code will work if you want values inside footer td tag.

var footerGrid = htmlDoc.DocumentNode.SelectSingleNode("//tfoot//td");
if (footerGrid != null)
        {
            var a = footerGrid.InnerText;
        }
Comments