Parsing HTML Table in C#

c# html-agility-pack html-table parsing

Question

I have a table on an html website that I want to parse using a C# windows form.

http://www.mufap.com.pk/payout-report.php?tab=01

This is the site I'm trying to parse.

> Foreach(Htmlnode a in document.getelementbyname("tr"))
{
    richtextbox1.text=a.innertext;
}

I attempted something similar, but since I am just printing all the trs, it won't provide me the information in tabular form. If you could assist me with this, I'm sorry for my English.

1
9
10/22/2012 5:11:13 AM

Accepted Answer

employing Agility Pack for HTML

WebClient webClient = new WebClient();
string page = webClient.DownloadString("http://www.mufap.com.pk/payout-report.php?tab=01");

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);

List<List<string>> table = doc.DocumentNode.SelectSingleNode("//table[@class='mydata']")
            .Descendants("tr")
            .Skip(1)
            .Where(tr=>tr.Elements("td").Count()>1)
            .Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToList())
            .ToList();
44
11/23/2017 2:37:32 PM

Popular Answer

Do you intend anything similar to this?

foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table")) {
    ///This is the table.    
    foreach (HtmlNode row in table.SelectNodes("tr")) {
    ///This is the row.
        foreach (HtmlNode cell in row.SelectNodes("th|td")) {
            ///This the cell.
        }
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow