本文介绍了HTML敏捷性包 - 问题选择子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要出口运行计划,iCal和自阿西斯不提供这种服务我的Asics,我决定为我个人的使用有点刮刀。我想要做的是从我计划中的所有预定运行和生成基于一个iCal供稿。我使用C#和HTML敏捷性包。

I want to export my Asics running plan to iCal and since Asics do not offer this service, I decided to build a little scraper for my own personal use. What I want to do is to take all the scheduled runs from my plan and generate an iCal feed based on that. I am using C# and Html Agility Pack.

我想要做的就是通过我的所有计划运行(他们是DIV节点)迭代。然后接下来我要选择我的节点运行几个不同的节点。我的code是这样的:

What I want to do is iterate through all my scheduled runs (they are div nodes). Then next I want to select a few different nodes with my run nodes. My code looks like this:

foreach (var run in doc.DocumentNode.SelectSingleNode("//div[@id='scheduleTable']").SelectNodes("//div[@class='pTdBox']"))
{
    number++;
    string date = run.SelectSingleNode("//div[@class='date']").InnerText;
    string type = run.SelectSingleNode("//span[@class='menu']").InnerHtml;
    string distance = run.SelectSingleNode("//span[@class='distance']").InnerHtml;
    string description = run.SelectSingleNode("//div[@class='description']").InnerHtml;
    ViewData["result"] += "Dato: " + date + "<br />";
    ViewData["result"] += "Tyep: " + type + "<br />";
    ViewData["result"] += "Distance: " + distance + "<br />";
    ViewData["result"] += "Description: " + description + "<br />";
    ViewData["result"] += run.InnerHtml.Replace("<", "&lt;").Replace(">", "&gt;") + "<br />" + "<br />" + "<br />";
}

我的问题是, run.SelectSingleNode(// DIV [@类='日期'])。的InnerText 内不选择与给定的XPath节点在给定的运行节点。它选择整个文件中匹配的XPath的第一个节点。

My problem is that run.SelectSingleNode("//div[@class='date']").InnerText does not select the node with the given XPath within the given run node. It selects the first node that matches the XPath in the entire document.

我如何可以选择单个节点与当前节点中给定的XPath?

How can I select the single node with the given XPath within the current node?

感谢您。

更新

我试图更新我的XPath字符串这样:

I tried updating my XPath string to this:

string date = run.SelectSingleNode(".div[@class='date']").InnerText;

这应该选择&LT; D​​IV CLASS =日期&GT;&LT; / DIV&GT; 当前节点内的元素,对不对?好吧,我试过,但得到这个错误:

This should select the <div class="date"></div> element within the current node, right? Well, I tried this but got this error:

防爆pression结果必须为
  节点集。说明:未处理
  期间发生的异常
  当前Web请求的执行。
  有关详情,请堆栈跟踪
  有关该错误以及信息
  它起源于code。

异常详细信息:
  System.Xml.XPath.XPathException:
  防爆pression结果必须为
  节点集。

Exception Details: System.Xml.XPath.XPathException: Expression must evaluate to a node-set.

有什么建议?

推荐答案

有几件事情用的 HtmlAgilityPack 的XPath 的前pressions工作时,这将帮助你。

A few things that will help you when working with HtmlAgilityPack and XPath expressions.

如果运行 HtmlNode ,则:


  1. run.SelectNodes(// DIV [@类='日期'])
    结果的意志会完全一样 doc.DocumentNode.SelectNodes(// DIV [@类='日期'])

run.SelectNodes(./ DIV [@类='日期'])
结果会给你所有的&LT; D​​IV&GT; 节点是运行节点的孩子。它不会搜索更深,只在第二天深度级别。

run.SelectNodes("./div[@class='date']")
Will give you all the <div> nodes that are children of run node. It won't search deeper, only at the very next depth level.

run.SelectNodes(.// DIV [@类='日期'])
结果将返回所有在&LT; D​​IV&gt;在该类属性节点,但不是唯一的旁边运行节点,也将深入查找(它的每一个可能的后裔)

run.SelectNodes(".//div[@class='date']")
Will return all the <div> nodes with that class attribute, but not only next to the run node, but also will search in depth (every possible descendant of it)

您将有2或3之间进行选择,具体取决于满足你的需要:)

You will have to choose between 2. or 3., depending on which one satisfy your needs :)

这篇关于HTML敏捷性包 - 问题选择子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 02:29