Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
3.57/5 (14 votes)
15 Apr 20052 min read 61.3K   933   33   10
This load the "ECB" xml file and parse it into a dataset. From that we can calculated the Exchange rate between difference countries.
<p\>

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


Written By
Software Developer (Senior) Similix ApS
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 6.0, SQL Server, C# and .NET. The Similix ApS is a IT konsult house and working with GIS (Esri).
In my work, I have gained extensive experience with web security. ADFS etc..
Have also gained experience with DevOps CI and automatic deployment

Comments and Discussions

 
QuestionHI, I modified the parseWebXml.... Pin
sushiBite6-Dec-11 4:33
sushiBite6-Dec-11 4:33 
AnswerRe: HI, I modified the parseWebXml.... Pin
hestol8-Dec-11 0:31
hestol8-Dec-11 0:31 
Generalprevious days Pin
AndrusM7-Jan-11 10:09
AndrusM7-Jan-11 10:09 
AnswerRe: previous days Pin
hestol16-Jan-11 23:19
hestol16-Jan-11 23:19 
GeneralRe: previous days Pin
AndrusM16-Jan-11 23:26
AndrusM16-Jan-11 23:26 
AnswerRe: previous days Pin
hestol17-Jan-11 2:31
hestol17-Jan-11 2:31 
GeneralGreat, but... Pin
gggggg29-Nov-05 0:33
gggggg29-Nov-05 0:33 
GeneralPictures are not displayed Pin
totig6-Apr-05 1:07
totig6-Apr-05 1:07 
Hi, thanks for the article - I have been looking to do something like this for a while, but have just been to lazy. Anyway, just letting you know the pictures are not being displayed.
GeneralRe: Pictures are not displayed Pin
hestol15-Apr-05 2:46
hestol15-Apr-05 2:46 
GeneralRe: Pictures are not displayed Pin
drutch6-Dec-07 5:45
drutch6-Dec-07 5:45 

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

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