相关疑难解决方法(0)

将数据从HTML表中获取到数据表中

好的,我需要查询实时网站以从表中获取数据,将此HTML表放入DataTable然后使用此数据.到目前为止,我已经设法使用Html Agility Pack和XPath来获取我需要的表中的每一行,但我知道必须有一种方法可以将其解析为DataTable.(C#)我目前使用的代码是:

string htmlCode = "";
using (WebClient client = new WebClient())
{
htmlCode = client.DownloadString("http://www.website.com");
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

doc.LoadHtml(htmlCode);

//My attempt at LINQ to solve the issue (not sure where to go from here)
var myTable = doc.DocumentNode
.Descendants("table")
.Where(t =>t.Attributes["summary"].Value == "Table One")
.FirstOrDefault();

//Finds all the odd rows (which are the ones I actually need but would prefer a
//DataTable containing all the rows!
foreach (HtmlNode cell in doc.DocumentNode.SelectNodes("//tr[@class='odd']/td"))
{
string test = …
Run Code Online (Sandbox Code Playgroud)

html c# linq xpath html-agility-pack

5
推荐指数
2
解决办法
6万
查看次数

标签 统计

c# ×1

html ×1

html-agility-pack ×1

linq ×1

xpath ×1