2

I am using c# to read (attempting to) an RSS feed, but I am getting an error "Namespace prefix 'cb' is not defined" , I am pretty new to XML and C# and was hoping for some help, I read a bit on creating the Namespace but I am not 100% sure I am grasping it.

Any help would be greatly appreciated.

The C# code is:

 /*
      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);
        }
    }

and the summarized version of the rss is:

<?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>
2
  • Why have you decided to use old-style XmlDocument? It would be much easier with XDocument and LINQ to XML :) Commented Jan 8, 2014 at 19:38
  • The only reason is because I was looking for an RSS to SSIS example and this is what popped up as an example, beyondrelational.com/modules/24/syndicated/398/Posts/9954/… heh Commented Jan 8, 2014 at 19:50

1 Answer 1

4

You need to add "cb" to your XmlNamespaceManager in order to use "cb" in SelectSingleNode.

nsmgr.AddNamespace(
    "cb", 
    "http://www.cbwiki.net/wiki/index.php/Specification_1.1");
Sign up to request clarification or add additional context in comments.

5 Comments

Also, I would recommend that you use currencyElement.HasAttribute("about", "http://www.w3.org/1999/02/22-rdf-syntax-ns#") instead of currencyElement.HasAttribute("rdf:about")
It worked perfectly, thank you. I did not know i could add multiple namespaces.
I changed the HasAttribute as well, and that also works.. however I don't understand why it works. since the www.w3.org is referencing the cb and not the rdf:
You've got it backwards. "cb" is referencing "http://www.cbwiki.net/wiki/index.php/Specification_1.1" and "rdf" is referencing "http://www.w3.org/1999/02/22-rdf-syntax-ns#".
ahh, that would be why.. sorry long day. Thanks again for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.