Parse html table using HtmlAgilityPack (Linq)

html-agility-pack html-table linq

Question

Here's the table structure:

<table class="tb-stock tb-option">
<tr>
    <th class="bgc2">col1</th>
    <th class="bgc2">col2</th>
    <th class="bgc2">col3</th>
</tr>
<tr class="alt-row">
    <th class="">2018/1/29</th>
    <td class="">0.11</td>
    <td class=" b-b">0.50</td>
</tr>
<tr class="alt-row">
    <th class="">2018/1/30</th>
    <td class="">0.22</td>
    <td class=" b-b">0.55</td>
</tr>
</table>

I want to get all the elements below "tr" (including "th" and "td")

How can I use linq to achieve this ?

Problems locate at "..tr.Elements("td|th").."

code:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(ms, Encoding.UTF8);

List<List<string>> table = 
doc.DocumentNode.SelectSingleNode("//table[@class='tb-stock tb-option']")
                .Descendants("tr")
                .Skip(1)
                .Where(tr => tr.Elements("th").Count() >= 1)
                .Select(tr => tr.Elements("td|th").Select(td => td.InnerText).ToList())
                .ToList();

Accepted Answer

You can use the following code for extracting inner texts of td or th elements I test it in my local the output is :

2018/1/29
0.11
0.50
2018/1/30
0.22
0.55

You can filter the elements in line :

// both td and th
.Where(node => "td|th".Contains(node.Name))
// only td
.Where(node => "td".Contains(node.Name))

The working code is :

HtmlDocument doc = new HtmlDocument();
doc.Load("test.html", Encoding.UTF8);

List<string> table =
doc.DocumentNode.SelectSingleNode("//table[@class='tb-stock tb-option']")
    .Descendants("tr")
    .Skip(1)
    .Where(tr => tr.Elements("th").Count() >= 1)
    .SelectMany(tr => tr.ChildNodes)
    .Where(node => "td|th".Contains(node.Name))
    .Select(node => node.InnerText)
    .ToList();

foreach (var str in table)
{
    Console.WriteLine(str);
}


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