-1

我想从 XML 中获取英镑兑美元的汇率值。

代码是这样的

System.Xml.XPath.XPathDocument path = new System.Xml.XPath.XPathDocument(url);
System.Xml.XPath.XPathNavigator nav = path.CreateNavigator();
System.Xml.XPath.XPathNodeIterator itr = nav.Select("/Envelope/Cube/Cube");

这是XML

<?xml version="1.0" encoding="windows-1252"?>
<Envelope xmlns="http://www.gesmes.org/xml/2002-08-01">

  <Cube xmlns="http://www.bankofengland.co.uk/boeapps/iadb/agg_series" SCODE="XUDLUSS" DESC="Spot exchange rate, US $ into Sterling" COUNTRY="" CONCAT="Not seasonally adjusted # Exchange rates # US dollar # Exchange rate (spot) - US dollar into sterling # US dollar ">
    <Cube TIME="2013-07-22" OBS_VALUE="1.537" OBS_CONF="N" LAST_UPDATED="2013-07-23 09:30:00">
    </Cube>
    <Cube TIME="2013-07-23" OBS_VALUE="1.5367" OBS_CONF="N" LAST_UPDATED="2013-07-24 09:30:00">
    </Cube>
  </Cube>
</Envelope>
4

1 回答 1

2

您很可能需要添加带有前缀的命名空间,并将此前缀与您的 xpath 一起使用。看看:XPathNavigator.Select

并尝试这样的事情(未经测试)。

System.Xml.XPath.XPathDocument path = new System.Xml.XPath.XPathDocument(url);
System.Xml.XPath.XPathNavigator nav = path.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable);
manager.AddNamespace("e", "http://www.gesmes.org/xml/2002-08-01"");
manager.AddNamespace("c", "http://www.bankofengland.co.uk/boeapps/iadb/agg_series"); 
System.Xml.XPath.XPathNodeIterator itr = nav.Select("/e:Envelope/c:Cube/c:Cube", manager );
于 2013-07-24T10:20:59.503 回答