string input = @"<table>
<tr>
<td>Text A</td>
</tr>
<tr>
<td>
<table> <!-- Notice this is an inner scope table -->
<tr>
<td>Text B</td>
</tr>
</table>
</td>
</tr>
</table>
<table>
<tr>
<td>
<table> <!-- Notice this is an inner scope table -->
<tr>
<td>Text C</td>
</tr>
</table>
</td>
</tr>
</table>
<table>
<tr>
<td>Text D</td>
</tr>
</table>"我有一系列以上字符串格式的表。
我想在所有<tr>的第一级提取内容,其中预期提取的内容是:
Text A
<table>
<tr>
<td>Text B</td>
</tr>
</table>
<table>
<tr>
<td>Text C</td>
</tr>
</table>
Text D使用HtmlAgilityPack:
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(input);
var output = htmlDocument.DocumentNode
.SelectNodes("table/tr");里面的桌子正在被捡起来,而不是外面的桌子。我想不出该如何“确保只有父母tr的孩子才能被抓起来”。
发布于 2019-10-14 22:48:26
使用/启动/表达式,以显式指定相对于文档根的匹配,例如"/table/tr"。这样的表达式与预期的内容匹配,但与<td>标记相匹配。要实现与问题表达式中所写的完全相同的结果,应将其更新为"/table/tr/td"
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(input);
var output = htmlDocument.DocumentNode
.SelectNodes("/table/tr/td");
foreach (HtmlNode node in output)
{
Console.WriteLine(node.InnerHtml);
}https://stackoverflow.com/questions/58384757
复制相似问题