<p\>
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.
I Store the parsing data into a dataset, see picture.
From this dataset I can find and get data, so on.
Code
private ExchanceRate parseWebXML(string WebAddress)
{
XmlTextReader xmlReader;
ExchanceRate dsVa = new ExchanceRate();
DataRow newRowVa = null;
try
{
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 != "")
{
if (xmlReader.Name == "gesmes:name")
{
_author = xmlReader.ReadString();
}
for (int i = 0 ; i < xmlReader.AttributeCount; i++)
{
if (xmlReader.Name == "Cube")
{
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");
newRowCo["Rate"]= 1.0;
dsVa.Country.Rows.Add(newRowCo);
newRowCo.SetParentRow(newRowVa);
}
if (xmlReader.AttributeCount == 2)
{
xmlReader.MoveToAttribute("currency");
string cur = xmlReader.Value;
xmlReader.MoveToAttribute("rate");
decimal rat = decimal.Parse(xmlReader.Value.Replace(".",","));
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.
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