Parsing HTML Table in C#

user1764351 picture user1764351 · Oct 22, 2012 · Viewed 63.3k times · Source

I have an html page which contains a table and i want to parse that table in C# windows form

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

this is the webpage i want to parse i have tried

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

i have tried some thing like this but it wont give me in tabular form as i am simply printing all trs so please help me regarding this thanx sorry for my english.

Answer

L.B picture L.B · Oct 22, 2012

Using Html Agility Pack

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