ASP.NET currency converter using foxrate.org XML-RPC API

Abstract: In this article I will guide you through the process of creating simple currency converter using Foxrate.com XML RPC API. This API is free to use. There a few examples available in programming languages such as Python, PHP or RoR, however in ASP.NET C# its the first.

What we need: In order to connect to the XML-RPC API, you will need an XML-RPC client. First of all add latest version of “CookComputing.XmlRpc” to your project. I have used ver 2.5 in my project, since it has some bug fixed related to permission and security when running on live server.

Source: XML-RPC client DLL download link: http://xmlrpcnet.googlecode.com/files/xml-rpc.net.2.5.0.zip

References: Add following references in your code behind files:

using System.Collections;
using CookComputing.XmlRpc;

Now, the code behind activity by defining endpoint and creating an Interface.

XmlRpcUrl(“http://foxrate.org/rpc/”)]

public interface IXchangeRate

{

[XmlRpcMethod(“foxrate.currencyConvert”)]

XmlRpcStruct GetXchangerate(

string from,

string to,

double amount);  // Please note that Foxrate.org RPC accepts “Float” data type however, CookComputing.XmlRpc does not provide support for “Float”, thus we use double instead.

}

//Create function to fetch rates in real time.

public void GetRates(string strTo, string strFrom, double dAmount)

{

IXchangeRate proxy = (IXchangeRate)XmlRpcProxyGen.Create(typeof(IXchangeRate));

XmlRpcStruct MyStruct;

MyStruct = proxy.GetXchangerate(strTo, strFrom, dAmount);

foreach (DictionaryEntry d in MyStruct)

{

Response.Write(d.Key + ” : ” + d.Value);

}

}

Finally call the function as under:

GetRates(“INR”, “USD”, “10.0”);

You can also visit the working sample, I implemented on of  live site: http://shapemyyatra.com/currency-converter.aspx