【问题标题】:HTMLAgilityPack - selecting nodes from node collection - instead of documentNodeHTMLAgilityPack - 从节点集合中选择节点 - 而不是 documentNode
【发布时间】:2012-09-15 07:24:00
【问题描述】:

我的代码如下所示:

var matchTableHtml =
            loginPage.DocumentNode
            .SelectNodes("//table[@id='uc_refereeMatchProgram_dgMatchCategory1']
                          //tr[@class!='DataGridHeaderStyle']");

这会返回一个节点集合 - 正如预期的那样,所以没问题 - 但是!!!

var testing = matchTableHtml.Descendants()
            .Select(x => new Match()
                             {
                                 Row = x.SelectSingleNode("//td/a[position()=1]")
                                       .InnerText
                             });

这里的“x”从 documentNode 而不是从“matchTableHtml.Descendants”集合返回节点。谁知道为什么?

【问题讨论】:

    标签: c# html-agility-pack


    【解决方案1】:

    好的 - 解决方案非常简单...我不是从“当前节点”开始,但以 // 开头的 XPath 当然是相对于根节点的。

    简单的解决方案 - 添加一个点 - 表示我们从当前 ('//' ==> './/') 开始:

    Row = x.SelectSingleNode(".//td/a[position()=1]")
                                       .InnerText
    

    【讨论】:

      猜你喜欢
      • 2012-07-02
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多