2012-07-08 138 views
5

我想要做的是,從互聯網上獲得匯率。 經過長時間的研究,我發現了這個功能。從互聯網上獲得匯率

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"; 
     } 
    } 

http://www.webservicex.net/不支持AZN(阿塞拜疆馬納特)以美元,反之亦然轉換。我想要做的是,如果有可能連接到互聯網並獲得利率。否則使用書面函數進行轉換(我已經寫過)。

你有什麼建議,我怎樣才能得到美元和AZN的當前匯率(或只是通過發送美元或AZN得到結果)?無論如何要從Windows窗體應用程序中獲取它?

回答

0

http://www.webservicex.net/不支持AZN(阿塞拜疆馬納特)以美元,反之亦然

所以呢?計算通過其他貨幣的交叉匯率。

AZN可能是一種邊緣貨幣,其交易量或暴露量非常有限。詢問OANDA(http://www.oanda.com)我收到一些報價,包括美元轉換(http://www.oanda.com/currency/cross-rate/result?quotes=GBP &報價= EUR &報價= JPY &報價= CHF &報價= USD &報價= AZN &去=獲取+我+表+%3E)

可能的webservicesx.net只是沒有價格的東西,出來的主要貨幣。

使用另一個報價。 FXCM和Oanda可能會有API可供您訂購 - 可能與價格有關。

另一種方法是,您可以查看是否可以計算從AZN到另一種貨幣的交叉價格,如果有價格並從USD到USD。這在外匯交易中經常進行 - 雖然 - 令人滿意 - 美元大多不需要交叉匯率計算。

有沒有辦法從Windows窗體應用程序中獲取它?

當你問到互聯網上的API時,它是完全不相關的,無論是winforms,webforms,powershell還是VB腳本,API都支持它,你使用的UI技術是無關。

0

也許這會有所幫助。我谷歌,我看到一些替代的網絡服務,但我看到的不支持AZN。但是我沒有花費大量時間做這件事,那是你的工作。我沒有發現這樣的:
http://www.transfermate.com/en/free_currency_converter.asp

,您可以添加到您的應用程序,也許通過增加一個瀏覽器控件和一個自定義網頁上嵌入這個和檢索的結果,你的主要形式。但最終你自己回答了這個問題:

否則使用書面函數進行轉換(我已經寫過)。

如果您找不到已經存在的解決方案,請自行構建。

相關搜索: https://developers.google.com/finance/http://openexchangerates.org/

4

這個簡單的algorythm會給你所有你在一個鍵值對列表需要。

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; 
    } 
+0

有關更深入的解釋,請留言。 – Freeman 2012-07-09 11:57:04

+0

不錯的一段代碼,但是有沒有選擇通常的'Dictionary <>'的原因? – 2014-12-10 07:13:53