Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XPath不会选择应该选择的节点_Xpath - Fatal编程技术网

XPath不会选择应该选择的节点

XPath不会选择应该选择的节点,xpath,Xpath,我想从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中获得英镑兑美元的汇率值

代码是这样的

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>

很可能需要添加带有前缀的名称空间,并在xpath中使用此前缀。 看看:

试试这种没有经过测试的东西

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 );

到目前为止看起来不错。它返回什么?很可能您需要添加带有前缀的名称空间并使用它。它不会返回任何内容。我正在从英格兰银行检索此XML,无法更改它。我可以更改XPath表达式。怎么做?