Html Agility Pack,表节点上的迭代不起作用 [英] Html Agility Pack, iteration on table node not working
本文介绍了Html Agility Pack,表节点上的迭代不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下面的代码,它应该在一张表(页面上的第四个表)中循环遍历所有< td>
。
foreach(doc.DocumentNode.SelectNodes(// table)中的HtmlNode表。Skip(4).Take(1))
$ b Console.WriteLine(table.SelectNodes(// table)。Count()); // = 5?
Console.WriteLine(table.SelectNodes(// table)。Skip(4).Take(1).Count()); // = 1!
foreach(table.SelectNodes(// td)中的var td)
{
Console.WriteLine(td.InnerText);
$ b $ p $由于某些原因表格。 SelectNodes(// td)
遍历所有五个表< td>
的。
有什么想法可以解决这个问题?
解决方案您需要添加单点(<$ c $ (在这种情况下,相对于当前的表
):),以便将其识别为相对路径p>
foreach(table.SelectNodes(.// td)中的var td)
{
Console。的WriteLine(td.InnerText);
}
I have the following code which should iterate through all the <td>
's in just one table (the fourth one on the page).
foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table").Skip(4).Take(1))
{
Console.WriteLine(table.SelectNodes("//table").Count()); // = 5?
Console.WriteLine(table.SelectNodes("//table").Skip(4).Take(1).Count()); // = 1!
foreach (var td in table.SelectNodes("//td"))
{
Console.WriteLine(td.InnerText);
}
}
For some reason table.SelectNodes("//td")
is iterating through ALL five tables <td>
's.
Any ideas how I can correct this?
解决方案 You need to add single dot (.
) at the beginning of the XPath to make it recognized as relative path (in this case, relative to current table
) :
foreach (var td in table.SelectNodes(".//td"))
{
Console.WriteLine(td.InnerText);
}
这篇关于Html Agility Pack,表节点上的迭代不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文