2014-01-08 266 views
2

我正在使用c#来读取(试图)一个RSS源,但我收到一个错误"Namespace prefix 'cb' is not defined",我对XML和C#很新,希望得到一些帮助,我读了一下创建命名空间,但我不是100%确定我正在抓住它。XML名称空间前缀错误

任何帮助将不胜感激。

的C#代码:

/* 
     Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer". 
     For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput". 
    */ 
    // Create an XmlNamespaceManager to resolve the default namespace. 

    XmlDocument xm = new XmlDocument(); 
    xm.Load(Variables.USFeed.ToString()); 
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xm.NameTable); 
    nsmgr.AddNamespace("rdf", "http://purl.org/rss/1.0/"); 
    XmlNodeList xnode = xm.GetElementsByTagName("item"); 


    foreach (XmlNode xmn in xnode) 
    {    
     XmlElement currencyElement = (XmlElement)xmn; 
     if (currencyElement.HasAttribute("rdf:about")) 
     { 
      Output0Buffer.AddRow(); 
      Output0Buffer.observationPeriod = currencyElement.SelectSingleNode("cb:statistics/cb:exchangeRate/cb:observationPeriod", nsmgr).InnerText; 
      Output0Buffer.targetCurrency = currencyElement.SelectSingleNode("cb:statistics/cb:exchangeRate/cb:targetCurrency", nsmgr).InnerText; 
      Output0Buffer.baseCurrency = currencyElement.SelectSingleNode("cb:statistics/cb:exchangeRate/cb:baseCurrency", nsmgr).InnerText; 
      Output0Buffer.exchangeRate = double.Parse(currencyElement.SelectSingleNode("cb:statistics/cb:exchangeRate/cb:value", nsmgr).InnerText); 
     } 
    } 

和RSS的总结版本是:

<?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/assets/rates_rss/noon/en_ALL.xml"> 
     <title xml:lang="en">Bank of Canada: Noon Foreign Exchange Rates</title> 
     <link>http://www.bankofcanada.ca/rates/exchange/noon-rates-5-day/</link> 
     <description>Current day's noon foreign exchange rates from the Bank of Canada. Published at about 12:15 ET.</description> 
     <items> 
      <rdf:Seq> 
       <rdf:li rdf:resource="http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_USD.xml" /> 
       <rdf:li rdf:resource="http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_VEF.xml" /> 
<rdf:li rdf:resource="http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_VND.xml" /> 
      </rdf:Seq> 
     </items> 
    </channel> 
    <item rdf:about="http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_USD.xml"> 
     <title xml:lang="en">CA: 0.9382 USD = 1 CAD 2014-01-06 Bank of Canada noon rate</title> 
     <cb:statistics> 
      <cb:country>CA</cb:country> 
      <cb:exchangeRate> 
       <cb:value decimals="4">0.9382</cb:value> 
       <cb:baseCurrency>CAD</cb:baseCurrency> 
       <cb:targetCurrency>USD</cb:targetCurrency> 
       <cb:rateType>Bank of Canada noon rate</cb:rateType> 
       <cb:observationPeriod frequency="daily">2014-01-06T12:15:00-05:00</cb:observationPeriod> 
      </cb:exchangeRate> 
     </cb:statistics> 
    </item> 
    <item rdf:about="http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_ARS.xml"> 
     <title xml:lang="en">CA: 6.1843 ARS = 1 CAD 2014-01-06 Bank of Canada noon rate</title> 
     <cb:statistics> 
      <cb:country>CA</cb:country> 
      <cb:exchangeRate> 
       <cb:value decimals="4">6.1843</cb:value> 
       <cb:baseCurrency>CAD</cb:baseCurrency> 
       <cb:targetCurrency>ARS</cb:targetCurrency> 
       <cb:rateType>Bank of Canada noon rate</cb:rateType> 
       <cb:observationPeriod frequency="daily">2014-01-06T12:15:00-05:00</cb:observationPeriod> 
      </cb:exchangeRate> 
     </cb:statistics> 
    </item> 
+0

为什么你决定使用旧式'XmlDocument'?使用'XDocument'和LINQ to XML会更容易:) – MarcinJuraszek

+0

唯一的原因是因为我正在寻找一个RSS到SSIS的例子,这就是弹出的例子,http://beyondrelational.com/modules/ 24/syndicated/398/Posts/9954/ssis-how-to-pull-currency-rates-from-european-central-bank.aspx heh – jnoel10

回答

4

你需要 “CB” 添加到您的XmlNamespaceManager才能使用SelectSingleNode中的“cb”。

nsmgr.AddNamespace(
    "cb", 
    "http://www.cbwiki.net/wiki/index.php/Specification_1.1"); 
+1

此外,我建议您使用'currencyElement.HasAttribute(“about” “http://www.w3.org/1999/02/22-rdf-syntax-ns#”)''而不是'currencyElement.HasAttribute(“rdf:about”)' –

+0

它工作得很好,谢谢。我不知道我可以添加多个名称空间。 – jnoel10

+0

我也改变了HasAttribute,这也是有效的..但是我不明白它为什么起作用。因为www.w3.org引用的是cb而不是rdf: – jnoel10