请帮助XPath查询 - 在简单的XML文档上

本文关键字:XML 文档 简单 帮助 XPath 查询 | 更新日期: 2023-09-27 17:55:48

我有这个来自网络服务的简单XML文档

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"    xmlns="http://purl.org/rss/1.0/" xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3c.org/1999/02/22-rdf-syntax-ns#rdf.xsd">
<channel rdf:about="http://www.bankofcanada.ca/stats/">
<title xml:lang="en">Bank of Canada closing rate: U.S. dollar (close)</title> 
<link>www.bankofcanada.ca/</link> 
<description>Current day's closing foreign exchange rates from the Bank of Canada. Published at  about 4:15 ET.</description> 
 <items>
  <rdf:Seq>
   <rdf:li rdf:resource="http://www.bankofcanada.ca/stats/assets/rates_rss/STATIC_IEXE0102.xml" /> 
  </rdf:Seq>
 </items>
</channel>
<item rdf:about="http://www.bankofcanada.ca/stats/rates_rss/STATIC_IEXE0102.xml">
 <title xml:lang="en">CA: 1.0378 USD = 1 CAD 2011-05-09 Bank of Canada closing rate</title> 
 <link>http://www.bankofcanada.ca/rates/exchange/daily-closing-past-five-day/</link> 
 <description xml:lang="en">1 Canadian Dollar = 1.0378 USD (USD = U.S. dollar (close)). These are the Bank of Canada's nominal closing exchange rates, published at about 4:15 ET each business day. These are neither buying nor selling rates, and should be used for reference purposes only.</description> 
 <dc:date>2011-05-09T16:15:00-04:00</dc:date> 
 <dc:language>en</dc:language> 
 <cb:statistics>
  <cb:country>CA</cb:country> 
  <cb:exchangeRate> 
  <cb:value decimals="4">1.0378</cb:value> 
  <cb:baseCurrency>CAD</cb:baseCurrency> 
  <cb:targetCurrency>USD</cb:targetCurrency> 
  <cb:rateType>Bank of Canada closing rate</cb:rateType> 
  <cb:observationPeriod frequency="daily">2011-05-09T16:15:00-04:00</cb:observationPeriod> 
 </cb:exchangeRate>
</cb:statistics>
</item>
</rdf:RDF>

我正在尝试这样做

XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
manager.AddNamespace("cb", "http://centralbanks.org/cb/1.0/");
manager.AddNamespace("dcterms", "http://purl.org/dc/terms/");
manager.AddNamespace("xsi", "http://www.w3c.org/2001/XMLSchema-instance");
XmlNode valueNode = xmlDoc.SelectSingleNode("descendant::cb:value", manager);
string v = valueNode.InnerText;

以选择值节点,但它只返回 null。 它已经工作了几个月,但我想他们以某种方式更改了文档(但不幸的是我没有旧副本)。

非常感谢您的任何提示

请帮助XPath查询 - 在简单的XML文档上

我注意到 xml 中cb别名的命名空间是:

http://www.cbwiki.net/wiki/index.php/Specification_1.1

使用时:

http://centralbanks.org/cb/1.0/

对于命名空间管理器。

更新代码中的命名空间可能会解决此问题。