使用HtmlAgilityPack将HtmlTable填入DataTable
程序员文章站
2023-01-29 13:58:00
HtmlAgilityPack.HtmlWeb hw = new HtmlAgilityPack.HtmlWeb(); HtmlAgilityPack.HtmlDocument doc = hw.Load("C:\\Users\\u1\\Desktop\\test html.html"); Html... ......
htmlagilitypack.htmlweb hw = new htmlagilitypack.htmlweb(); htmlagilitypack.htmldocument doc = hw.load("c:\\users\\u1\\desktop\\test html.html"); htmlagilitypack.htmlnode rootnode = doc.documentnode;//获取文档的根节点 // //*[@id="report1"]/tbody string xpath = "//*[@id='report1']/tbody"; htmlagilitypack.htmlnode node = rootnode.selectsinglenode(xpath);//获取表格 foreach (var script in node.descendants("script").toarray()) script.remove(); foreach (var style in node.descendants("style").toarray()) style.remove(); string innertext = node.outerhtml;//到这里就是纯纯的表格了 var trnodes = node.selectnodes("tr"); datatable dt = new datatable(); for (int i = 0; i < 100; i++) //给表格增加100个列 { datacolumn dc = new datacolumn(); dt.columns.add(dc); } foreach (var trnod in trnodes) //遍历行 { //dt.rows.add() datarow dr = dt.newrow(); var tdnodes = trnod.selectnodes("td"); for (int i = 0; i < tdnodes.count; i++) //遍历列 { dr[i] = tdnodes[i].innertext; } dt.rows.add(dr); } // 所有的数据读取到了datatable 中
上一篇: 非我族类其心必异是怎样的心理
下一篇: oracle数据库逐步学习总结【基础一】