XPath 查询仅在 SingleNodeSelect 中的某些查询上始终返回 NULL

发布于 2024-09-09 22:46:23 字数 1238 浏览 7 评论 0原文

<?xml version="1.0" encoding="utf-16"?>
<users>
  <user number="0772247157">
    <step stepnumber="1">complete</step>
    <step stepnumber="2">complete</step>
    <step stepnumber="3">complete</step>
  </user>
  <user number="0772247158">
    <step stepnumber="1">complete</step>
    <step stepnumber="2">complete</step>
  </user>
  <user number="0772247159">
    <step stepnumber="1">complete</step>
  </user> 
</users>

等查询

//user[@number='0772243950']//user[@number=0772243950]/step[last()] 可以正常工作,不会出现任何问题使用 SelectSingleNode 方法。

但是以下函数始终返回 null。它与 XPath Visualizer 完美配合,并且我使用在线 XPath 评估器进行了双重检查。

public bool checkStepExists(string Number, string StepNumber)
{
    string XPathQuery = "//user[@number=" + Number + "]/step[@stepnumber=" + StepNumber + "]";

    XmlNode Search = SettingsFile.SelectSingleNode(XPathQuery);      

    if (Search == null)
       return false;
    else
       return true;            
}

在问这个问题之前我搜索了SO,所有内容都指向名称空间问题。但我无法理解的是,这是一个没有命名空间的本地 XML 文件。或者,我应该始终拥有命名空间和前缀并使用它吗?

<?xml version="1.0" encoding="utf-16"?>
<users>
  <user number="0772247157">
    <step stepnumber="1">complete</step>
    <step stepnumber="2">complete</step>
    <step stepnumber="3">complete</step>
  </user>
  <user number="0772247158">
    <step stepnumber="1">complete</step>
    <step stepnumber="2">complete</step>
  </user>
  <user number="0772247159">
    <step stepnumber="1">complete</step>
  </user> 
</users>

Queries such as

//user[@number='0772243950'] and //user[@number=0772243950]/step[last()] works without any trouble which uses SelectSingleNode method.

However the following function ALWAYS returns null. It works perfectly with XPath Visualizer and i double checked with an online XPath evaluator.

public bool checkStepExists(string Number, string StepNumber)
{
    string XPathQuery = "//user[@number=" + Number + "]/step[@stepnumber=" + StepNumber + "]";

    XmlNode Search = SettingsFile.SelectSingleNode(XPathQuery);      

    if (Search == null)
       return false;
    else
       return true;            
}

I searched on SO before asking this question and all points to namespace problems. But what I can't understand is that this is a local XML file which does not have a namespace. OR, should I ALWAYS have a namespace and a prefix and use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

带刺的爱情 2024-09-16 22:46:23

在您的字符串 XPathQuery 中,我认为您需要引用数字。
像这样:

string XPathQuery = "//user[@number=\"" + Number + "\"]/step[@stepnumber=\"" + StepNumber + "\"]";

Within your string XPathQuery I think you need to quote the numbers.
Like this:

string XPathQuery = "//user[@number=\"" + Number + "\"]/step[@stepnumber=\"" + StepNumber + "\"]";
初见你 2024-09-16 22:46:23

也许尝试使用额外的引号?

string XPathQuery = "//user[@number='" + Number + "']/step[@stepnumber='"
          + StepNumber + "']";

我还有兴趣检查 NumberStepNumber 周围是否添加了任何空格。基本上:您传入的实际字符串查询是什么? (连接后等)。

Perhaps try it with the extra quotes?

string XPathQuery = "//user[@number='" + Number + "']/step[@stepnumber='"
          + StepNumber + "']";

I would also be interested in checking if there is any whitespace adding around Number or StepNumber. Basically: what is the actual string query you pass in? (after concatenation etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文