0

HTML 正在使用内置 WebControl 的 WinForm 中显示

我决定尝试 HTMLAgilityPack。

var query = from table in doc.DocumentNode.SelectNodes("//table[@class='TABLEBORDER').Cast<HtmlNode>() 
            from row in table.SelectNodes("tr").Cast<HtmlNode>() 
            from cell in row.SelectNodes("th|td").Cast<HtmlNode>() 
            select new {Table = table.Id, CellText = cell.InnerText}; 

foreach(var cell in query) { 
    Console.WriteLine("{0}: {1}", cell.Table, cell.CellText); 
} 

我根据@LB更新了代码我得到以下输出

The thread '<No Name>' (0x1e94) has exited with code 0 (0x0).
: 
Target

: 
Triggerenabled?

: 
Account

: 
Passwordchanged?


: 
Error message(if any)

The thread '<No Name>' (0x2564) has exited with code 0 (0x0).

其他数据在 web 控件上清晰可见。

4

1 回答 1

1

该页面还有其他表,但我只对类为“TABLEBORDER”的表感兴趣。

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

var table = doc.DocumentNode.SelectSingleNode("//table[@class='TABLEBORDER']");

编辑

var res = table.Descendants("tr")
               .Select(tr => tr.Descendants("td")
                               .Select(td => td.InnerText)
                               .ToList())
               .ToList();

编辑2

foreach (List<string> tr in res)
{
    foreach (string td in tr)
    {
        Console.Write("[{0}] ", td);
    }
    Console.WriteLine();
}
于 2012-08-21T19:47:24.817 回答