Code to Create Own Currency Convertor

Lakhangarg
Posted by Lakhangarg under ASP.NET category on | Views : 3768
XML Based Currency Converter

Sample Code:

using System.Xml;
private void ReadXMLData(string SourceCountry, string DestCountry)
{
double Amount = 10;
string SourceCode = "";
double SourceCountryValue=0;
double DestCountryValue=0;
string DestCode = "";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("Convertor.xml"));
XmlNodeList xmlList= xmlDoc.GetElementsByTagName("Country");
for (Int16 iCount = 0; iCount <>
{
if (xmlList.Item(iCount).Attributes.GetNamedItem("name").InnerText == SourceCountry)
{
SourceCountryValue = Convert.ToDouble(xmlList.Item(iCount).Attributes.GetNamedItem("value").InnerText);
SourceCode = xmlList.Item(iCount).Attributes.GetNamedItem("CurrencyCode").InnerText;
}
else if (xmlList.Item(iCount).Attributes.GetNamedItem("name").InnerText == DestCountry)
{
DestCountryValue = Convert.ToDouble(xmlList.Item(iCount).Attributes.GetNamedItem("value").InnerText);
DestCode = xmlList.Item(iCount).Attributes.GetNamedItem("CurrencyCode").InnerText;
}
}
Response.Write(Amount.ToString()+ " "+SourceCode +"=" +Convert.ToString(Amount * (DestCountryValue / SourceCountryValue)));
}

Call the above Method Like This: ReadXMLData("UK", "India");

XML Sample:

 
<Convertor>
<Country name="USA" CurrencyCode="USD" value="1"/>
<Country name="India" CurrencyCode="INR" value="50.525"/>
<Country name="UK" CurrencyCode="GBP" value="0.6817"/>
<Country name="Canada" CurrencyCode="CAD" value="1.2447"/>
<Country name="Australia" CurrencyCode="AUD" value="1.4175"/>
<Country name="China" CurrencyCode="CNY" value="6.8369"/>
</Convertor>

Comments or Responses

Login to post response