none
Help on parsing Xml RRS feed

  • Question

  • I am trying to parse the next xml

    Euro Bank Rate change

    whatever I do I can't get the the rate for USA I get that the node is null for example :

    var nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
                nsmgr.AddNamespace("gesmes", "http://www.gesmes.org/xml/2002-08-01");
                XmlNode node = xmlDocument.SelectSingleNode("/gesmes:Envelope[@xmlns:gesmes=\"http://www.gesmes.org/xml/2002-08-01\"]/Cube/Cube[@time=\"2019-12-18\"]//Cube[1]", nsmgr);
                XmlNodeList nodes = xmlDocument.SelectNodes("//gesmes:Cube", nsmgr);
                XmlNodeList n2odes = xmlDocument.SelectNodes("/gesmes:Envelope/Cube/Cube/Cube", nsmgr);
    what do i miss?

    Thursday, December 19, 2019 1:29 PM

Answers

  • Hi want 2 Learn,

    I see that you are using c#.

    The best API to handle XML in the .Net Framework is LINQ to XML.

    Check it out below.

    void Main()
    {
    	XDocument doc = XDocument.Parse(@"<?xml version='1.0' encoding='ISO-8859-1'?>
    				<gesmes:Envelope xmlns='http://www.ecb.int/vocabulary/2002-08-01/eurofxref'
                     xmlns:gesmes='http://www.gesmes.org/xml/2002-08-01'>
    	<gesmes:subject>Reference rates</gesmes:subject>
    	<gesmes:Sender>
    		<gesmes:name>European Central Bank</gesmes:name>
    	</gesmes:Sender>
    	<Cube>
    		<Cube time='2019-12-18'>
    			<Cube rate='1.1115' currency='USD'/>
    			<Cube rate='121.81' currency='JPY'/>
    			<Cube rate='1.9558' currency='BGN'/>
    			<Cube rate='25.490' currency='CZK'/>
    			<Cube rate='7.4720' currency='DKK'/>
    			<Cube rate='0.85055' currency='GBP'/>
    			<Cube rate='330.77' currency='HUF'/>
    			<Cube rate='4.2706' currency='PLN'/>
    			<Cube rate='4.7760' currency='RON'/>
    			<Cube rate='10.4483' currency='SEK'/>
    			<Cube rate='1.0913' currency='CHF'/>
    			<Cube rate='137.20' currency='ISK'/>
    			<Cube rate='10.0353' currency='NOK'/>
    			<Cube rate='7.4486' currency='HRK'/>
    			<Cube rate='69.5288' currency='RUB'/>
    			<Cube rate='6.5732' currency='TRY'/>
    			<Cube rate='1.6227' currency='AUD'/>
    			<Cube rate='4.5097' currency='BRL'/>
    			<Cube rate='1.4624' currency='CAD'/>
    			<Cube rate='7.7783' currency='CNY'/>
    			<Cube rate='8.6566' currency='HKD'/>
    			<Cube rate='15558.78' currency='IDR'/>
    			<Cube rate='3.8930' currency='ILS'/>
    			<Cube rate='78.8950' currency='INR'/>
    			<Cube rate='1296.56' currency='KRW'/>
    			<Cube rate='21.0900' currency='MXN'/>
    			<Cube rate='4.5999' currency='MYR'/>
    			<Cube rate='1.6920' currency='NZD'/>
    			<Cube rate='56.271' currency='PHP'/>
    			<Cube rate='1.5075' currency='SGD'/>
    			<Cube rate='33.606' currency='THB'/>
    			<Cube rate='15.9700' currency='ZAR'/>
    		</Cube>
    	</Cube>
    </gesmes:Envelope>");
    
    	XNamespace ns = doc.Root.GetDefaultNamespace();
    
    	var xElem = doc.Descendants(ns + "Cube").Elements(ns + "Cube").Elements(ns + "Cube").Where(d => d.Attribute("currency").Value.Equals("USD"));
    	string rate = xElem.Single().Attribute("rate").Value;
    }
    

    Thursday, December 19, 2019 2:30 PM

All replies

  • Hi want 2 Learn,

    I see that you are using c#.

    The best API to handle XML in the .Net Framework is LINQ to XML.

    Check it out below.

    void Main()
    {
    	XDocument doc = XDocument.Parse(@"<?xml version='1.0' encoding='ISO-8859-1'?>
    				<gesmes:Envelope xmlns='http://www.ecb.int/vocabulary/2002-08-01/eurofxref'
                     xmlns:gesmes='http://www.gesmes.org/xml/2002-08-01'>
    	<gesmes:subject>Reference rates</gesmes:subject>
    	<gesmes:Sender>
    		<gesmes:name>European Central Bank</gesmes:name>
    	</gesmes:Sender>
    	<Cube>
    		<Cube time='2019-12-18'>
    			<Cube rate='1.1115' currency='USD'/>
    			<Cube rate='121.81' currency='JPY'/>
    			<Cube rate='1.9558' currency='BGN'/>
    			<Cube rate='25.490' currency='CZK'/>
    			<Cube rate='7.4720' currency='DKK'/>
    			<Cube rate='0.85055' currency='GBP'/>
    			<Cube rate='330.77' currency='HUF'/>
    			<Cube rate='4.2706' currency='PLN'/>
    			<Cube rate='4.7760' currency='RON'/>
    			<Cube rate='10.4483' currency='SEK'/>
    			<Cube rate='1.0913' currency='CHF'/>
    			<Cube rate='137.20' currency='ISK'/>
    			<Cube rate='10.0353' currency='NOK'/>
    			<Cube rate='7.4486' currency='HRK'/>
    			<Cube rate='69.5288' currency='RUB'/>
    			<Cube rate='6.5732' currency='TRY'/>
    			<Cube rate='1.6227' currency='AUD'/>
    			<Cube rate='4.5097' currency='BRL'/>
    			<Cube rate='1.4624' currency='CAD'/>
    			<Cube rate='7.7783' currency='CNY'/>
    			<Cube rate='8.6566' currency='HKD'/>
    			<Cube rate='15558.78' currency='IDR'/>
    			<Cube rate='3.8930' currency='ILS'/>
    			<Cube rate='78.8950' currency='INR'/>
    			<Cube rate='1296.56' currency='KRW'/>
    			<Cube rate='21.0900' currency='MXN'/>
    			<Cube rate='4.5999' currency='MYR'/>
    			<Cube rate='1.6920' currency='NZD'/>
    			<Cube rate='56.271' currency='PHP'/>
    			<Cube rate='1.5075' currency='SGD'/>
    			<Cube rate='33.606' currency='THB'/>
    			<Cube rate='15.9700' currency='ZAR'/>
    		</Cube>
    	</Cube>
    </gesmes:Envelope>");
    
    	XNamespace ns = doc.Root.GetDefaultNamespace();
    
    	var xElem = doc.Descendants(ns + "Cube").Elements(ns + "Cube").Elements(ns + "Cube").Where(d => d.Attribute("currency").Value.Equals("USD"));
    	string rate = xElem.Single().Attribute("rate").Value;
    }
    

    Thursday, December 19, 2019 2:30 PM
  • thanks

    if any one know's how to solve it using XMLDocument will be interesting too

    Thursday, December 19, 2019 4:22 PM
  • Hi want 2 Learn,

    Glad to hear that the proposed solution is working for you.

    • LINQ to XML API is more than ten years old.
    • XMLDocument was in the .Net Framework before LINQ to XML advent, and kept there for backward compatibility for very old applications. It is better to stay away from it.

    Please don't forget to click "Mark as Answer" the response(s) that resolved your issue. This can be beneficial to other community members reading this thread.

    Thursday, December 19, 2019 4:28 PM
  • If you are interested in XPath as well, then check some examples:

    var nsmgr = new XmlNamespaceManager( xmlDocument.NameTable );
    nsmgr.AddNamespace( "d", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref" );
    nsmgr.AddNamespace( "g", "http://www.gesmes.org/xml/2002-08-01" );
     
    XmlNode first_cube = xmlDocument.SelectSingleNode( "/g:Envelope/d:Cube/d:Cube/d:Cube[1]", nsmgr );
    XmlNode first_cube_with_time = xmlDocument.SelectSingleNode( "/g:Envelope/d:Cube/d:Cube[@time='2019-12-19']/d:Cube[1]", nsmgr );
    XmlNode usd_cube = xmlDocument.SelectSingleNode( "/g:Envelope/d:Cube/d:Cube/d:Cube[@currency='USD']", nsmgr );
    string usd_rate_as_text = xmlDocument.SelectSingleNode( "/g:Envelope/d:Cube/d:Cube/d:Cube[@currency='USD']/@rate", nsmgr )?.Value;




    • Edited by Viorel_MVP Thursday, December 19, 2019 8:54 PM
    Thursday, December 19, 2019 6:49 PM