在C#中解析HTML表

use*_*351 8 c# parsing html-table html-agility-pack

我有一个包含表格的html页面,我想用C#窗体表格解析该表格

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

这是我想解析的网页我试过了

> Foreach(Htmlnode a in document.getelementbyname("tr"))
{
    richtextbox1.text=a.innertext;
}
Run Code Online (Sandbox Code Playgroud)

我尝试过这样的事情,但它不会以表格形式给我,因为我只是打印所有trs所以请帮助我关于这个thanx抱歉我的英语.

L.B*_*L.B 40

使用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();
Run Code Online (Sandbox Code Playgroud)

  • Thanx LB删除[@ class ='mydata'后]它作为通用代码...完美.... (2认同)

Nou*_*uny 10

你的意思是这样的吗?

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.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)