XPath 查询仅在 SingleNodeSelect 中的某些查询上始终返回 NULL
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的字符串
XPathQuery
中,我认为您需要引用数字。像这样:
Within your string
XPathQuery
I think you need to quote the numbers.Like this:
也许尝试使用额外的引号?
我还有兴趣检查
Number
或StepNumber
周围是否添加了任何空格。基本上:您传入的实际字符串查询是什么? (连接后等)。Perhaps try it with the extra quotes?
I would also be interested in checking if there is any whitespace adding around
Number
orStepNumber
. Basically: what is the actual string query you pass in? (after concatenation etc).