【问题标题】:System.Xml.XPath.XPathException: 'Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.'System.Xml.XPath.XPathException: '需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。
【发布时间】:2019-10-05 16:55:42
【问题描述】:

昨天我发布了这个问题:

https://stackoverflow.com/questions/56182368/html-agility-pack-select-node-after-particular-paragraph

我使用解决方案解决了:htmlSnippet.DocumentNode.SelectNodes('//p[text()="targetWord"]/following-sibling::ul[1]//li')

今天在另一个具有相同配置和 HTML 敏捷包版本的项目中,我收到标题中的错误消息。我能做些什么来解决它?

System.Xml.XPath.XPathException: 'Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.'

引发异常的代码:

var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(htmlText);


            //var ulCollection = htmlDoc.DocumentNode.SelectNodes("//ul");

            var tasksNodesCollection = htmlDoc.DocumentNode.SelectNodes("//p[TextValue()=\"Paragraph:\"]/following-sibling::ul[1]//li");

【问题讨论】:

  • 可以在这里找到解决方案 "stackoverflow.com/questions/13511527/…"
  • 这如何应用于 html 敏捷包?
  • 能否提供出错的代码
  • 您需要发布回复。看起来响应是 XML,您无法使用响应 xml 创建文档
  • var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(htmlText); //var ulCollection = htmlDoc.DocumentNode.SelectNodes("//ul"); var tasksNodesCollection = htmlDoc.DocumentNode.SelectNodes("//p[TextValue()=\"Paragraph:\"]/following-sibling::ul[1]//li");

标签: c# xml html-agility-pack


【解决方案1】:

您要查找的文本中有一个冒号。

htmlDoc.DocumentNode.SelectNodes("//p[TextValue()=\"Paragraph:\"]

解析器认为它是一个 XML 命名空间限定符。对冒号使用转义形式 "。

【讨论】:

  • 我也有删除冒号的问题
  • //p/following-sibling::ul[1]//li 表达式有效。问题是当我添加 textvalue 时也没有分号。也许我在 c# 字符串中转义了错误的双引号?
  • 已解决!!!我用 text()= 而不是 textvalue()= 我不知道为什么昨天它起作用了!!!
【解决方案2】:

解决了!!!我用 text()= 而不是 textvalue()= 我不知道为什么昨天它起作用了!!!

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 2014-04-04
    • 1970-01-01
    • 2011-01-26
    • 2023-03-04
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    相关资源
    最近更新 更多