我有这样的数据:
<tr class=hdr>
<th class="al cf">Name</th>
<th class="al">Type</th>
<th class="ar">Used Drive Space</th>
<th class="ar cl">Drive Size</th>
</tr>
<tr class="first o">
<td class="al cf">ITPHOFPWRFL01B E: Label:LotusDomino </td>
<td class="al">drive space</td>
<td class="ar">489.39106GB</td>
<td class="ar cl">549.9971GB</td>
</tr>
HTML文件中的该文件,我想这样读取该文件(例如在Notepad中读取HTML文件(,然后我想开始从该first o
读取,并且我想要的数据是ITPHOFPWRFL01B E: || 489.9106GB || 549.9971GB ..
。如何获取数据?
您应该使用html解析器,例如htmlagilitypack。
您可以使用以下代码使用HtmlAgilityPack
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "HTML files|*.html;*.htm";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(openFileDialog.FileName);
foreach (var node in doc.DocumentNode.SelectNodes("//*[@class='first o']"))
{
foreach(var node2 in node.SelectNodes(".//td"))
{
txtContent.Text += node2.InnerHtml + " || ";
}
}
}
}