SelectSingleNode 返回 null

发布于 2024-09-12 12:08:37 字数 1724 浏览 7 评论 0 原文

所以我有一个如下所示的 XML 文档:

<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
   <gesmes:subject>Reference rates</gesmes:subject>
   <gesmes:Sender>
       <gesmes:name>European Central Bank</gesmes:name>
   </gesmes:Sender>
   <Cube>
       <Cube time="2010-05-28">
           <Cube currency="USD" rate="1.2384"/>
           <Cube currency="JPY" rate="113.06"/>
       </Cube>
       <Cube time="2010-05-27">
           <Cube currency="USD" rate="1.2255"/>
           <Cube currency="JPY" rate="110.79"/>
       </Cube>
   </Cube>
</gesmes:Envelope>

现在假设我有一个 XmlNode timeNode 指向 < /code> 节点和指向加载的 XML 文档的 document。假设我需要通过调用 来获取 节点中 rate 属性的值 这里

到目前为止,我能够想出以下代码:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("ecb", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
nsmgr.AddNamespace("gesmes", "http://www.gesmes.org/xml/2002-08-01");


XmlNode currencyNode = timeNode.SelectSingleNode("descendant::ecb:Cube[@ecb:currency='USD']", nsmgr);

string rate = currencyNode.Attributes.GetNamedItem("rate").Value;

的问题是 currencyNode 设置为 null 在这里。 timeNode 并且它指向正确的节点,所以我猜问题出在 SelectSingleNode 方法中的路径,但我看不出问题出在哪里。检查了具有类似问题的其他帖子,但找不到任何可以解决地雷问题的内容。

So i have an XML document that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
   <gesmes:subject>Reference rates</gesmes:subject>
   <gesmes:Sender>
       <gesmes:name>European Central Bank</gesmes:name>
   </gesmes:Sender>
   <Cube>
       <Cube time="2010-05-28">
           <Cube currency="USD" rate="1.2384"/>
           <Cube currency="JPY" rate="113.06"/>
       </Cube>
       <Cube time="2010-05-27">
           <Cube currency="USD" rate="1.2255"/>
           <Cube currency="JPY" rate="110.79"/>
       </Cube>
   </Cube>
</gesmes:Envelope>

Now suppose that I have an XmlNode timeNode that points to <Cube time="2010-05-28"> node and a document that points to the loaded XML document. Lets say I need to get the value of the rate attribute in the <Cube currency=USD" rate="1.2384"/> node by calling a SelectSingleNode(string xpath) method.

So far I was able to come up with this code:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("ecb", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
nsmgr.AddNamespace("gesmes", "http://www.gesmes.org/xml/2002-08-01");


XmlNode currencyNode = timeNode.SelectSingleNode("descendant::ecb:Cube[@ecb:currency='USD']", nsmgr);

string rate = currencyNode.Attributes.GetNamedItem("rate").Value;

The problem here is that currencyNode is set to null here. I've checked the timeNode and it points to the correct node, so I guess the problem is with the path in the SelectSingleNode method, yet I fail to see where the problem is. I've checked other posts with similar problem but couldn't find anything that would solve mines. Any pointers will be appreciated.

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

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

发布评论

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

评论(2

只为一人 2024-09-19 12:08:37

将你的 xpath 更改为

descendant::ecb:Cube[@currency="USD"]

Change your xpath to

descendant::ecb:Cube[@currency="USD"]
坠似风落 2024-09-19 12:08:37

默认情况下,XML 属性没有命名空间,因此您不需要在它们上使用命名空间前缀。尝试一下:

XmlNode currencyNode = timeNode.SelectSingleNode("descendant::ecb:Cube[@currency='USD']", nsmgr);

您也不需要在此处显式指定后代轴,因为它默认会查看子项,因此您也可以将其缩短为:

XmlNode currencyNode = timeNode.SelectSingleNode("ecb:Cube[@currency='USD']", nsmgr);

XML attributes don't have the a namespace by default, so you don't need to use the namespace prefix on them. Try just:

XmlNode currencyNode = timeNode.SelectSingleNode("descendant::ecb:Cube[@currency='USD']", nsmgr);

You also don't need to explicitly specify the descendant axis here since it will look at children by default, so you could also shorten it to:

XmlNode currencyNode = timeNode.SelectSingleNode("ecb:Cube[@currency='USD']", nsmgr);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文