hi,
A Web Forms Currency Converter
Figure 5-9 shows a Web form that performs currency conversions using exchange rates stored in an XML file. To see it in action, copy Converter.aspx and Rates.xml, which are listed in Figures 5-10 and 5-11, to \Inetpub\wwwroot and type http://localhost/converter.aspx in your browser's address bar. Then pick a currency, enter an amount in U.S. dollars, and click the Convert button to convert dollars to the currency of your choice.
Here are some points of interest regarding the source code:
- Because it uses the DataSet class defined in the System.Data namespace, Converter.aspx begins with an @ Import directive importing System.Data.
- Rather than show a hard-coded list of currency types in the list box, Converter.aspx reads them from Rates.xml. Page_Load reads the XML file and initializes the list box. To add new currency types to the application, simply add new Rate elements to Rates.xml. They'll automatically appear in the list box the next time the page is fetched.
- For good measure, Converter.aspx wires the Convert button to the Click handler named OnConvert programmatically rather than declaratively. The wiring is done in Page_Init.
Notice how easily Converter.aspx reads XML from Rates.xml. It doesn't parse any XML; it simply calls ReadXml on a DataSet and provides an XML file name. ReadXml parses the file and initializes the DataSet with the file's contents. Each Rate element in the XML file becomes a row in the DataSet, and each row, in turn, contains fields named "Currency" and "Exchange". Enumerating all the currency types is a simple matter of enumerating the DataSet's rows and reading each row's "Currency" field. Retrieving the exchange rate for a given currency is almost as easy. OnConvert uses DataTable.Select to query the DataSet for all rows matching the currency type. Then it reads the Exchange field from the row returned and converts it to a decimal value with Convert.ToDecimal.
One reason I decided to use a DataSet to read the XML file is that a simple change would enable the Web form to read currencies and exchange rates from a database. Were Converter.aspx to open the XML file and parse it using the FCL's XML classes, more substantial changes would be required to incorporate database input.
A word of caution regarding this Web form: Don't use it to perform real currency conversions! The exchange rates in Rates.xml were accurate when I wrote them, but they'll be outdated by the time you read this. Unless you devise an external mechanism for updating Rates.xml in real time, consider the output from Converter.aspx to be for educational purposes only.
Converter.aspx
<%@ Import Namespace=System.Data %>
<html>
<body>
<h1>Currency Converter</h1>
<hr>
<form runat="server">
Target Currency<br>
<asp:ListBox ID="Currencies" Width="256" RunAt="server" /><br>
<br>
Amount in U.S. Dollars<br>
<asp:TextBox ID="USD" Width="256" RunAt="server" /><br>
<br> <asp:Button Text="Convert" ID="ConvertButton" Width="256"
RunAt="server" /><br>
<br>
<asp:Label ID="Output" RunAt="server" />
</form>
</body>
</html>
<script language="C#" runat="server">
void Page_Init (Object sender, EventArgs e)
{
// Wire the Convert button to OnConvert
ConvertButton.Click += new EventHandler (OnConver t);
}
void Page_Load (Object sender, EventArgs e)
{
// If this isn't a postback, initialize the ListBox
if (!IsPostBack) {
DataSet ds = new DataSet ();
ds.ReadXml (Server.MapPath ("Rates.xml"));
foreach (DataRow row in ds.Tables[0].Rows)
Currencies.Items.Add (row["Currency"].ToS tring ());
Currencies.SelectedIndex = 0;
}
}
void OnConvert (Object sender, EventArgs e)
{
// Perform the conversion and display the results
try {
decimal dollars = Convert.ToDecimal (USD.Text );
DataSet ds = new DataSet ();
ds.ReadXml (Server.MapPath ("Rates.xml"));
DataRow[] rows = ds.Tables[0].Select
("Curren cy = '" +
Currencies.SelectedItem.Text + "'");
decimal rate = Convert.ToDecimal (rows[0]
["Ex change"]);
decimal amount = dollars * rate;
Output.Text = amount.ToString ("f2");
}
catch (FormatException) {
Output.Text = "Error";
}
}
</script>
Rates.xml
<?xml version="1.0"?>
<Rates>
<Rate>
<Currency>British Pound</Currency>
<Exchange>0.698544</Exchange>
</Rate> <Rate>
<Currency>Canadian Dollar</Currency>
<Exchange>1.57315</Exchange>
</Rate>
<Rate>
<Currency>French Franc</Currency>
<Exchange>7.32593</Exchange>
</Rate>
<Rate>
<Currency>German Mark</Currency>
<Exchange>2.18433</Exchange>
</Rate>
<Rate>
<Currency>Italian Lira</Currency>
<Exchange>2162.67</Exchange>
</Rate>
<Rate>
<Currency>Japanese Yen</Currency>
<Exchange>122.742</Exchange>
</Rate>
<Rate>
<Currency>Mexican Peso</Currency>
<Exchange>9.22841</Exchange>
</Rate>
<Rate>
<Currency>Swiss Franc</Currency>
<Exchange>1.64716</Exchange>
</Rate>
</Rates>