Click here to Skip to main content
Click here to Skip to main content

WEBCLIENT to “European Central Bank” to calculated Exchange between countries.

, 15 Apr 2005
Rate this:
Please Sign up or sign in to vote.
This load the "ECB" xml file and parse it into a dataset. From that we can calculated the Exchange rate between difference countries.

Sample image

Introduction

The ECB very conveniently (and for free) gives daily the exchange rates of the most

common currencies, in several file formats, among which an XML file for parsing. (Shortened copy below)

<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time='2004-03-11'>
<Cube currency='USD' rate='1.2256'/>
<Cube currency='JPY' rate='135.58'/>
<Cube currency='DKK' rate='7.4518'/>
....
<Cube currency='ZAR' rate='8.2441'/>
</Cube>
</Cube>
</gesmes:Envelope>

Since this is a free resource, then this allow me to post also the URI for the XML file:
http://www.ecb.int/stats/eurofxref/eurofxref-hist.xml

I will make a Windows Application there was working as a Web Client to "ECB" XML.

From real rate of exchange from European Central Bank making a calculator to calculated the rate of Exchange between difference countries.

Background

The basic ideas are to get make a program to calculated the rate between difference countries. The European Central Bank dispose the a XML file over the typical

trade countries exchange rate. From this I can calculated how many money I have to pay for a product. Just remember, that we can only get the

exchange rate for the actual day when Bank is closed about 4 o'clock.

Using the code

Microsoft working with an XmlTextReader there can read from any kind of XML file.

But the ECB xml file is not a real web services so I want to parse it to a Dataset descript se below.

On this side http://www.oanda.com/site/help/iso_code.shtml can the index lists currency ISO codes by

country and precious metal. You can also find an ISO currency code by code. See picture how Web Client works.

 

Sample image

I Store the parsing data into a dataset, see picture.

From this dataset I can find and get data, so on.

Sample image

Code

private ExchanceRate parseWebXML(string WebAddress)
{
	XmlTextReader xmlReader;

	ExchanceRate dsVa = new ExchanceRate();

	DataRow newRowVa = null;

	try
	{
		//Read data from the XML-file over the interNET
		xmlReader = new XmlTextReader(WebAddress);
	}
	catch( WebException )
	{
		throw new WebException("There has been a mistake with collect the xmlfile from the net!");
	}

	try
	{
		while( xmlReader.Read() )
		{
			if (xmlReader.Name != "")
			{
				//Check that there are node call gesmes:name
				if (xmlReader.Name == "gesmes:name") 
				{
					_author = xmlReader.ReadString();
				}
						
				for (int i = 0 ; i < xmlReader.AttributeCount; i++)
				{
					//Check that there are node call Cube
					if (xmlReader.Name == "Cube") 
					{
						//Check that there are 1 attribut, then get the date
						if (xmlReader.AttributeCount == 1)
						{
							xmlReader.MoveToAttribute("time");

							DateTime tim = DateTime.Parse(xmlReader.Value);
							newRowVa = null;
							DataRow newRowCo = null;

							newRowVa = dsVa.Exchance.NewRow();
							newRowVa["Date"]= tim;
							dsVa.Exchance.Rows.Add(newRowVa);

							newRowCo = dsVa.Country.NewRow();
							newRowCo["Initial"]= "EUR";
							newRowCo["Name"]= convert.MoneyName("EUR");		// Find Country name from ISO code
							newRowCo["Rate"]= 1.0;
							dsVa.Country.Rows.Add(newRowCo);

							newRowCo.SetParentRow(newRowVa);	// Make Key to subtable
						}

						//If the number of attributs are 2, so get the ExchangeRate-node
						if (xmlReader.AttributeCount == 2)
						{
							xmlReader.MoveToAttribute("currency");
							string cur = xmlReader.Value;
                  
							xmlReader.MoveToAttribute("rate");
							decimal rat = decimal.Parse(xmlReader.Value.Replace(".",",")); // I am using "," as a decimal symbol

							DataRow newRowCo = null;

							newRowCo = dsVa.Country.NewRow();
							newRowCo["Initial"]= cur;
							newRowCo["Name"]= convert.MoneyName(cur);
							newRowCo["Rate"]= rat;
							dsVa.Country.Rows.Add(newRowCo);

							newRowCo.SetParentRow(newRowVa);	
						}

					xmlReader.MoveToNextAttribute();
				}
			}
		}
	}
	catch( WebException )
	{
		throw new WebException("connections lost");
	}

	return dsVa;
}

Here I can calculated the Exchange between difference countries from the Exchange rate.

private static double ExRateValue(double Input, double FromContry, double ToContry)
{
	double result = 0;
	if( FromContry == 0 ) return 0;
	result = (Input/FromContry)*ToContry;
	result = Math.Round(result,2);

	return result;
}

Conclusion

In this article has been shown one simple way how to implement a solution for connecting over Internet.

I used the power of .Net Technologies such as Web client in the XmlTextReader.

Remember that using this link it get data back from start of the Europe Central Bank history 01/04/1999,

and on mine computer it take ½ second to load the xml and parse the data to mine dataset.

Good luck.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

hestol
Software Developer (Senior) Bluegarden A/S
Denmark Denmark
I have working with C since 1988, and updates to VC++ in 1998. The first software I was making in VC++ was Fan calculation program with graph. This Fan Was integrated with the Oracle system.
I've been working almost exclusively with C# and .NET since
beginning of the technology.
Now I am working with Web API and CRM, MVC 5.0, SQL Server 2012 and .NET 2013. The company Bluegarden A/S is a IT/Finans house.

Comments and Discussions

 
QuestionHI, I modified the parseWebXml.... PinmembersushiBite6-Dec-11 5:33 
AnswerRe: HI, I modified the parseWebXml.... Pinmemberhestol8-Dec-11 1:31 
Generalprevious days PinmemberAndrusM7-Jan-11 11:09 
AnswerRe: previous days Pinmemberhestol17-Jan-11 0:19 
GeneralRe: previous days PinmemberAndrusM17-Jan-11 0:26 
AnswerRe: previous days Pinmemberhestol17-Jan-11 3:31 
GeneralGreat, but... Pinmembergggggg29-Nov-05 1:33 
GeneralPictures are not displayed Pinmembertoticow6-Apr-05 2:07 
GeneralRe: Pictures are not displayed Pinmemberhestol15-Apr-05 3:46 
GeneralRe: Pictures are not displayed Pinmemberdrutch6-Dec-07 6:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 15 Apr 2005
Article Copyright 2005 by hestol
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid