我正在使用c#来读取(尝试)RSS提要,但我收到错误"Namespace prefix 'cb' is not defined"
,我对XML和C#很陌生,并希望得到一些帮助,我读了一些关于创建命名空间,但我不是100%肯定我抓住了它。
非常感谢任何帮助。
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 :(得分:4)
您需要在XmlNamespaceManager中添加“cb”才能在SelectSingleNode中使用“cb”。
nsmgr.AddNamespace(
"cb",
"http://www.cbwiki.net/wiki/index.php/Specification_1.1");