Develop dynamic, data driven, mobile applications quickly and correctly
Click for a free download

^ click above ^
06.04.03

By Waheed Khan


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 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);


Resources for ASP Developers
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());
}
}


Click Here to Read the Full Article

About the Author:
Waheed Khan is currently working with ASP.NET, ADO.NET, XML, and C#

 




-- WebProAsp is an ">iEntry, Inc. publication --
2003 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal