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

时间:2011-05-10 19:23:54

标签: c# xml xpath

我有一个来自webservice的简单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。它已经工作了几个月,但我想他们以某种方式改变了文档(但遗憾的是我没有旧版本)。

非常感谢任何提示

1 个答案:

答案 0 :(得分:4)

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

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

在使用时:

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

用于命名空间管理器。

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