我希望从XML获得GBP兑换美元的汇率。
代码就像这样
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>
答案 0 :(得分: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 );