Jeff Prosise has written an article "http://webdeveloper.earthweb.com/webjs/print.php/10959_982451_5", he pretty much explained how to load XML data with ASP.NET from the "Rates.xml" file. In this article I have created Currency Converter Server which can be scheduled to extract the data from third party site and 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 exchange rate. But if you look at the countries names they are not alphabetically sorted, I will explain this later how to sort data within 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>
.
.
.
.
.
SOURCE FROM : http://www.csharphelp.com/archives2/archive441.html