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






3.57/5 (14 votes)
Apr 5, 2005
2 min read

62011

936
This load the "ECB" xml file and parse it into a dataset. From that we can calculated the Exchange rate between difference countries.
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.
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. I Store the parsing data into a dataset, see picture. From this dataset I can find and get data, so on. Here I can calculated the Exchange between difference countries from the Exchange rate.
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.Using the code
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;
}
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