heron
heron

Reputation: 3661

Getting exchange rates from the internet

What I wanna do is, to get exchange rates from internet. I found this function after long research.

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
        string xmlResult = null;
        string url;
        url = "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=" + TextBox1.Text + "&ToCurrency=" + TextBox2.Text + "";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader resStream = new StreamReader(response.GetResponseStream());
        XmlDocument doc = new XmlDocument();
        xmlResult = resStream.ReadToEnd();
        doc.LoadXml(xmlResult);
        Label1.Text = "Current Exchange Rate for " + TextBox1.Text.ToUpper() + " ---> " + TextBox2.Text.ToUpper() + " value " + doc.GetElementsByTagName("double").Item(0).InnerText;
        }
        catch(Exception ex)
        {
            Label1.Text="Not a valid Currency or Try again later";
        }
    } 

But http://www.webservicex.net/ doesn't support AZN (Azerbaijani Manat) to usd and vice versa conversion. What I wanna do is, if it's possible connect to the internet and get rates. Else use written function for conversion (I'VE already written).

What do you advice, how can I get current rates for USD and AZN (or just get result by sending USD or AZN) ? Is there anyway to get it from inside Windows forms application?

Upvotes: 5

Views: 12138

Answers (3)

Freeman
Freeman

Reputation: 5801

This simple algorythm will give you all that you need in a key value pair list.

public static List<KeyValuePair<string, decimal>> GetCurrencyListFromWeb(out DateTime   currencyDate)
    {
        List<KeyValuePair<string, decimal>> returnList = new List<KeyValuePair<string, decimal>>();
        string date = string.Empty;
        using (XmlReader xmlr = XmlReader.Create(@"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"))
        {
            xmlr.ReadToFollowing("Cube");
            while (xmlr.Read())
            {
                if (xmlr.NodeType != XmlNodeType.Element) continue;
                if (xmlr.GetAttribute("time") != null)
                {
                    date = xmlr.GetAttribute("time");
                }
                else returnList.Add(new KeyValuePair<string, decimal>(xmlr.GetAttribute("currency"), decimal.Parse(xmlr.GetAttribute("rate"), CultureInfo.InvariantCulture)));
            }
            currencyDate = DateTime.Parse(date);
        }
        returnList.Add(new KeyValuePair<string, decimal>("EUR", 1));
        return returnList;
    }

Upvotes: 5

GrayFox374
GrayFox374

Reputation: 1782

Maybe this will help. I Google'd and I did see some alternative web services, but the ones I looked at did not support AZN. But I didn't spend a ton of time doing that, that is your job. I did find this:
http://www.transfermate.com/en/free_currency_converter.asp

which you can add to your application, maybe by adding a browser control and embedding this on a custom page and retrieving the results to your main form. But ultimately, you answered the question yourself:

Else use written function for conversion (I'VE already written).

If you can't find a solution already out there, build it yourself.

Also try: https://developers.google.com/finance/ and http://openexchangerates.org/

Upvotes: 0

TomTom
TomTom

Reputation: 62127

But http://www.webservicex.net/ doesn't support AZN (Azerbaijani Manat) to usd and vice versa

So? Calculate a cross rate going through another currency.

AZN is likely a fringe currency with very limited volume or exposure. Asking OANDA (http://www.oanda.com) I am getting some quotes, including a USD conversion (http://www.oanda.com/currency/cross-rate/result?quotes=GBP&quotes=EUR&quotes=JPY&quotes=CHF&quotes=USD&quotes=AZN&go=Get+my+Table+%3E)

Likely webservicesx.net just has no prices for something that out of the main currencies.

Use another quote. FXCM and Oanda may have API's you can subscribe to - likely against a price.

Alternative you can see whether you can calculate a cross - go from AZN to another currency if there is a price and from there to USD. This is done frequently in FOREX though - agreeable - the USD mostly is not in the need of a cross rate calculation.

Is there anyway to get it from inside Windows forms application?

When you ask about an API on the internet, then it is totally irrelevant whether it is winforms, webforms, powershell or a vb script, either the API supports it, or not, and the UI Technology you use is irrelevant.

Upvotes: 0

Related Questions