c#表格的html解析器 [英] c# html parser for tables
本文介绍了c#表格的html解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望将html文件中的数据转换为字典。 html文件包含几个表。但每张桌子只包含2个colloumns。我已经尝试过html-aglility-pack解析器,但我找不到任何有用的关于表的纪录片。以下是html文件的一部分:
i want to get data from an html file into a Dictionary. The html file contains several tables. But every table contains just 2 colloumns. I have tried already the html-aglility-pack parser but i cant find any useful documentary about tables. Here is a part of the html file:
<TR><TD><TD><TD COLSPAN=3><B>CPU Physical Info:</B>
<TR><TD><TD><TD><TD>Package Type <TD>775 Contact FC-LGA8
<TR><TD><TD><TD><TD>Package Size <TD>37.5 mm x 37.5 mm
<TR><TD><TD><TD><TD>Transistors <TD>456 million
<TR><TD><TD><TD><TD>Process Technology <TD>45 nm, CMOS, Cu, High-K Gate
<TR><TD><TD><TD><TD>Die Size <TD>164 mm2
<TR><TD><TD><TD><TD>Core Voltage <TD>1.100 - 1.262 V
<TR><TD><TD><TD><TD>I/O Voltage <TD>1.100 - 1.262 V
<TR><TD><TD><TD><TD>Typical Power <TD>95 W @ 2.83 GHz
<TR><TD>
谢谢
Thank you
推荐答案
参考 - 在C#中创建字典或字符串列表(包含HTML标签) [ ^ ]。一个例子就是。
Refer- create a dictionary or list from string(HTML tag included) in C#[^]. One example is there.
Quote:
你应该使用 HTML Agility Pack 。
例如:(已测试)
var doc = new HtmlDocument();
doc.LoadHtml(s);
var dict = doc.DocumentNode.Descendants("tr")
.ToDictionary(
tr => int.Parse(tr.Descendants("td").First().InnerText),
tr => int.Parse(tr.Descendants("td").Last().InnerText)
);
如果HTML总是格式正确,你可以使用LINQ-to-XML;代码几乎相同。
If the HTML will always be well-formed, you can use LINQ-to-XML; the code would be almost identical.
这篇关于c#表格的html解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文