Currency Converter Server with C#

Introduction:

Jeff Prosise has written an article "Currency Converter with ASP.NET Web Forms",he pretty much explained how to load XML data with ASP.NET from the"Rates.xml" file. In this article I have created Currency ConverterServer which can be scheduled to extract the data from third party siteand build the "Rates.xml" dynamically.

CurrencyConverter.dll Component:

"CurrencyConverter.dll" This component will extract the data from PACIFIC Exchange site using regular expression patterns.

// html for which we are writing this regular expression pattern
/*
<TD align=middle>ADP</TD>
<TD align=left>Andorran Peseta</TD>
<TD align=right>100.92</TD>
<TD align=right>152.844</TD>
<TD align=right>166.386</TD>
<TD align=right>251.917</TD>
*/
Regex regex = new Regex(@"<td.*?>(.+)</td>+", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

MatchCollection matches = regex.Matches(rawHtml);

if (matches.Count > 0)
{
// Get the first match
foreach(Match match in matches)
{
if (match.Success)
{
// Get the second group in the match
Group grp = match.Groups[1];

// return data
Console.WriteLine(grp.Value);
}

}
}

Once we extract the data from the site then we can build "Rates.xml" XML file using XmlTextWriter class.

XmlTextWriter writer = new XmlTextWriter(XMLFilePath, System.Text.Encoding.UTF8);

writer.WriteStartDocument();

//Use automatic indentation for readability.
writer.Formatting = Formatting.Indented;

//Write the root element
writer.WriteStartElement("Rates");

//Start an element
writer.WriteStartElement("Rate");

//add sub-elements
writer.WriteElementString("Currency", "British Pound");
writer.WriteElementString("Exchange", "0.635243");

//End the item element
writer.WriteEndElement(); // end Rate

// end the root element
writer.WriteFullEndElement();

//Write the XML to file and close the writer
writer.Flush();
writer.Close();

Test file which will connect to the PACIFIC Exchange site and extract the data and build "Rates.xml" file.

class BuildXML
{
static void Main()
{

string SupplementaryExchangeRatesActualSiteUrl = "http://pacific.commerce.ubc.ca/xr/rates.html";
Currency cc = new Currency();
cc.ExchangeRatesURL = SupplementaryExchangeRatesActualSiteUrl;
cc.XMLFilePath = "Rates.xml";
cc.Build();
Console.WriteLine(cc.ModuleError.ToString());
}
}

Here is the some site, from which exchange rate data can be extracted.

Here is the some site, from which exchange rate data can be extracted.

PACIFIC Exchange Rate Service for 242 countries
http://pacific.commerce.ubc.ca/xr/rates.html

PACIFIC Exchange daily Rates but limited countries
http://pacific.commerce.ubc.ca/xr/today.html

US Treasury Exchange Rates
http://www.fms.treas.gov/intn.html

Federal Reserve Bank Exchange Rates
http://www.federalreserve.gov/releases/H10/Update/

International Monetary Fund (IMF) Exchange Rates
http://www.imf.org/external/np/tre/sdr/db/rms_five.cfm

Rates.xml file:

"Rates.xml" contains 242 countries exchangerate. But if you look at the countries names they are notalphabetically sorted, I will explain this later how to sort datawithin ASP.NET page.

<?xml version="1.0" encoding="utf-8"?>
<Rates>
<Rate>
<Currency>Andorran Peseta</Currency>
<Exchange> 154.763</Exchange>
</Rate>
<Rate>
<Currency>U.A.E. Dirham</Currency>
<Exchange> 3.673</Exchange>
</Rate>
<Rate>
<Currency>Afghanistan Afghani</Currency>
<Exchange> 42.785</Exchange>
</Rate>
<Rate>
<Currency>Albanian Lek</Currency>
<Exchange> 129.181</Exchange>
</Rate>
<Rate>
<Currency>Armenia Dram</Currency>
<Exchange> 554.882</Exchange>
</Rate>
<Rate>
<Currency>Neth. Ant. Guilder</Currency>
<Exchange> 1.78</Exchange>
</Rate>
<Rate>
<Currency>Angolan Kwanza</Currency>
<Exchange> 65.0163</Exchange>
</Rate>
<Rate>
<Currency>Argentine Peso</Currency>
<Exchange> 3.155</Exchange>
</Rate>
<Rate>
<Currency>Austrian Schilling</Currency>
<Exchange> 12.7991</Exchange>
</Rate>
<Rate>
<Currency>Christmas Is. Dollar</Currency>
<Exchange> 1.65399</Exchange>
</Rate>
<Rate>
<Currency>Cocos(Keeling) Is.</Currency>
<Exchange> 1.65399</Exchange>
</Rate>
.
.
.
.
.
.

Compiling and building your Component:

Zip file contains "CurrencyConverter.cs","BuildXML.cs", and "CurrencyConverter.aspx". And I have also includedcompile.bat, rate.make, and Rates.xml. By running the compile.bat fileat the command prompt it will create "CurrencyConverter.dll" componentand it will also build "BuildXML.exe" file, by executing the"BuildXML.exe" at the command prompt it will connect to PACIFICExchange site and extract the data and build us "Rates.xml" file. Whichcan then be read by our asp.net page "Converter.aspx" in a browser.Converter.aspx ASP.NET page explanation:

Jeff Prosise pretty much explain this portionof the asp.net page in his article. Earlier I mentioned that"Rates.xml" file data is not sorted. To sort the Country names in theListBox is easy. Once we fill the DataView then we can use Sortproperty of the DataView Class to sort the data in the ListBox control.

<%@ Page Language="C#" %>
<%@ Import Namespace=System.Data %>

<script runat="server">

void Page_Load(object sender, System.EventArgs e)
{
// If this isn't a postback, initial
ize the ListBox
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath ("Rates.xml"));

DataView dv = ds.Tables[0].DefaultView;
dv.Sort = "Currency ASC";

lstboxCurrency.DataSource = dv;
lstboxCurrency.SelectedIndex = 0;
lstboxCurrency.DataTextField = "Currency";
lstboxCurrency.DataBind();
}
}

// Perform the conversion and display the results
void btnConvert_Click(object sender, System.EventArgs e)
{

try
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("Rates.xml"));
DataRow[] rows = ds.Tables[0].Select("Currency = '" + lstboxCurrency.SelectedItem.Text + "'");

decimal CurrencyAmount = Convert.ToDecimal(Amount.Text);
decimal rate = Convert.ToDecimal(rows[0]["Exchange"]);
decimal amount = CurrencyAmount * rate;
Result.Text = amount.ToString("f2");
}
catch (FormatException)
{
Errors.Text = "Error";
}

}

void Page_Init (Object sender, EventArgs e)
{
// Wire the Convert button to OnConvert
btnConvert.Click += new System.EventHandler(btnConvert_Click);
Load += new System.EventHandler(Page_Load);
}

</script>

ASP.NET page output:

output Currency Converter Server with C#

Note:
In this article PACIFIC Exchange Rate Service data is strictly used for educational purpose only and should not be used in any commercial application.

Most Commented Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!