【发布时间】:2018-12-17 13:18:11
【问题描述】:
我有以下 HTML 示例文档:
.....
<div class="TableElement">
<table>
<tr>
<th class="boxToolTip" title="La quotazione di A2A è in rialzo o in ribasso?"> </th>
..
<th class="boxToolTip" class="ColumnLast" title="Trades più recenti su A2A">Ora <img title='' alt='' class='quotePageRTupgradeLink' href='#quotePageRTupgradeContainer' id='cautionImageEnt' src='/common/images/icons/caution_sign.gif'/></th>
</tr>
<tr class="odd">
..
<td align="center"><span id="quoteElementPiece6" class="PriceTextUp">1,619</span></td>
<td align="center"><span id="quoteElementPiece7" class="">1,6235</span></td>
<td align="center"><span id="quoteElementPiece8" class="">1,591</span></td>
<td align="center"><span id="quoteElementPiece9" class="">1,5995</span></td>
..
</tr>
</table>
</div>
......
我需要在 quoteElementPiece 6、7、8、9 和 17(目前在文档中)部分获取对应的值。
我现在只是在代码中一一搜索:
int index6 = doc.IndexOf("quoteElementPiece6");
..
int index17 = doc.IndexOf("quoteElementPiece17");
我想通过一次性扫描并拥有我需要的子字符串的所有索引来改进这一点。示例:
var searchstrings = new string[]
{
"quoteElementPiece6",
"quoteElementPiece7",
"quoteElementPiece8",
"quoteElementPiece9",
"quoteElementPiece17"
};
int[] indexes = getIndexes(document,searchstrings); //indexes should be sorted accordingly to the order in searchstrings
.NET 中是否有任何本机功能(例如 LinQ)?
我知道有 HTML Parser 库,但我更喜欢避免使用这些库,我想了解如何为每种文档执行此操作。
【问题讨论】:
-
请至少显示您在 Google 上搜索过的内容?.... 更简单的搜索结果如下:C# Is there a LINQ to HTML, or some other good .Net HTML manipulation API?
-
我想避免使用第三方库或解析整个 HTML,HTML 文档只是一个示例。
-
您可以使用 Linq To XML,但这需要格式良好的 HTML,您可以使用 SgmlReader 获得该 HTML。你也可以使用 HtmlAgilityPack。