所以我有一个如下所示的 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.
发布评论
评论(2)
将你的 xpath 更改为
Change your xpath to
默认情况下,XML 属性没有命名空间,因此您不需要在它们上使用命名空间前缀。尝试一下:
您也不需要在此处显式指定后代轴,因为它默认会查看子项,因此您也可以将其缩短为:
XML attributes don't have the a namespace by default, so you don't need to use the namespace prefix on them. Try just:
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: