xpath查询返回空值 [英] xpath Query return null value

查看:126
本文介绍了xpath查询返回空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Header>
      <reference>
        <identifier>BatchProcessed</identifier>
        <datetime>2011-08-15T05:12:00+10:00</datetime>
      </reference>
    </Header>
    <Header>
      <reference>
        <identifier>Running</identifier>
        <datetime>2011-08-15T05:12:00+10:00</datetime>
      </reference>
    </Header>


我的xpath查询是


my xpath query is

XmlNode node3 = xdoc.SelectSingleNode("//identifier/text()");



该查询假定获取第一个标识符值"BatchProcessed".

但是当我测试时返回null,但是当我在xml工具(在线)中使用路径时,它会重新运行值.

我不知道这里缺少什么,有人启发了我.



the query suppose to get the first identifier value " BatchProcessed".

but it returns null when i test but when i use the path in xml tool (online) it retruns value.

i don''t know what am missing here,some one enlighten me

推荐答案

您不应使用text().只需获取节点,然后在代码中获取节点的值即可. XPath的含义可能会有所不同.使用//也很浪费,如果XML增长了,您会发现它比在要搜索的路径中显示得更慢.
You should not use text(). Just get the node, and then get the value of the node in code. XPath implimentations can vary. Using // is also wasteful, if your XML grows, you will find that it''s slower than being more explicit in the path you want to search.


因为您想要第一个标识符要显示的值,您可以通过以下方式实现:

Since you want the first identifier''s value to be shown, you can do it in this way:

string value = xdoc.SelectSingleNode("//identifier[position()=1]").InnerText; //Returns BatchProcessed



希望对您有所帮助.



Hope this helps.


将其显示为

xdoc.SelectSingleNode("//* [local-name()=''ServiceId'']"));
figured it out

xdoc.SelectSingleNode("//*[local-name()=''ServiceId'']");


这篇关于xpath查询返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆