循环节点集合为我提供了唯一节点,但从这些节点中选择节点为我提供了第一个循环项的结果

Looping a node collection gives me unique nodes but selecting nodes inside from these give me the results of the first loop item

上下文:使用 HTMLAgilityPack 库,我循环 HtmlNodeCollection,打印节点的 HTML 为我提供了我需要的数据,但是当我在 [=23] 中选择节点时=],它们都给我选择节点的第一项的结果。

将节点 html 写为 node.InnerHtml 给我它们的唯一 html,全部正确,但是当我执行 SelectSingleNode 时,它​​们都给我相同的数据。

由于项目原因,我不能透露网站。我能说的是有 17 个节点,它们都是 div 和 class k-user-item。所有项目都是独一无二的,这意味着它们都是不同的。

感谢您的帮助!

代码:

var nodes = w.DocumentNode.SelectNodes("//div[contains(@class, 'k-user-item')]");

List<Sales> saleList = new List<Sales>();

foreach (HtmlNode node in nodes)
{
    //This line prints correct html, selecting single nodes gives me always the same data of the first item from the loop.
    //Debug.WriteLine(node.InnerHtml);


    string payout = node.SelectSingleNode("//*[@class=\"k-item--buy-date\"]").InnerText;
    string size = node.SelectSingleNode("//*[@class=\"k-panel-title\"]").SelectNodes("//span")[1].InnerText;
    var trNodes = node.SelectNodes("//tr");

    string status = trNodes[1].SelectSingleNode("//b").InnerText;
    string orderId = trNodes[2].SelectNodes("//td")[1].SelectSingleNode("//span").InnerHtml;
    string sellDate = node.SelectSingleNode("//*[@class=\"k-panel-heading\"]").SelectNodes("//small")[1].InnerHtml;

}

通过向 XPath 添加“.”解决了这个问题。开始了。

不在 XPath 上添加点意味着该节点将在整个文档中搜索,而不仅仅是确切的节点 html。