raysefo raysefo - 1 year ago 55
HTML Question

HTML Agility Pack - retrieving multiple values from TD & TR tags

I am new to HTML agility pack, there are some values on a web page I need to get but couldn't findout how to. Hope you can help me.

Best regards.

Sample HTML portion:

...
<div id="page-bgtop">
<div id="page-bgbtm">
<div id="content">
<div class="post">
<div class="entry">
<center>
<div/>
<div>
<table class="CSSTableGenerator" cellspacing="0" cellpadding="4" border="0" id="GridView2" style="width: 98%; border-collapse: collapse;">
<tbody>
<tr style="font-weight: bold;">
<tr style="background-color: rgb(239, 243, 251);">
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">506</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">29/06/2017</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">4</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">20</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">21</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">32</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">43</a>
</td>
<td>
<a href="Super-Loto-Cekilis-Sonucu.aspx?ID=506.Hafta-29/06/2017-Super-Loto-Sonucu">47</a>
</td>
</tr>
<tr style="background-color: white;">
<td>
...


Values I would like to get:
506
29/06/2017
4
20
21
32
43
47

PS: This is just a sample html portion, there are many rows of value.

Answer Source

Use the XPath descendant-or-self axis (//), specifically //tr to select the all table rows, then check the row(s) for hyperlinks in each cell:

var document = new HtmlDocument();
document.LoadHtml(html);
//               ^^^^^^ your HTML snippet above
var rows = document.DocumentNode.SelectNodes("//tr");

if (rows != null && rows.Count > 0)
{
    foreach (var row in rows)
    {
        var links = row.SelectNodes("./td/a");
        if (links != null) Console.WriteLine(string.Join(" ", links.Select(x => x.InnerText)));

    }
}

Output:

506 29/06/2017 4 20 21 32 43 47

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download