C# 从互联网上获取汇率
我想做的是,从互联网上获取汇率。 经过长时间的研究,我发现了这个功能C# 从互联网上获取汇率,c#,windows,winforms,visual-studio-2010,visual-studio,C#,Windows,Winforms,Visual Studio 2010,Visual Studio,我想做的是,从互联网上获取汇率。 经过长时间的研究,我发现了这个功能 protected void Button1_Click(object sender, EventArgs e) { try { string xmlResult = null; string url; url = "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRa
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(阿塞拜疆马纳特)兑换美元,反之亦然。我想做的是,如果可能的话,连接到互联网并获得价格。否则使用write函数进行转换(我已经编写了)
你有什么建议,我怎样才能得到美元和AZN的当前汇率(或者仅仅通过发送美元或AZN获得结果)?是否仍然可以从Windows窗体应用程序内部获取它
但不支持AZN(阿塞拜疆马纳特)兑换美元,反之亦然
那么?计算通过另一种货币的交叉汇率
AZN可能是一种边缘货币,交易量或风险敞口非常有限。问奥达(http://www.oanda.com)我得到一些报价,包括美元兑换(http://www.oanda.com/currency/cross-rate/result?quotes=GBP"es=EUR"es=JPY"es=CHF"es=USD"es=AZN&go=Get+我的+表格+%3E)
很可能webservicesx.net没有主要货币以外的东西的价格
使用另一个报价。FXCM和Oanda可能有您可以订阅的API-可能是有价格限制的
另一种选择是,如果有价格,你可以看看你是否可以计算从AZN到另一种货币的交叉价格,然后从那里到美元。这在外汇交易中是经常发生的,但令人满意的是,美元基本上不需要交叉汇率计算
是否仍然可以从Windows窗体应用程序内部获取它
当您询问internet上的API时,无论它是winforms、webforms、powershell还是vb脚本都是无关紧要的,API是否支持它,您使用的UI技术也是无关紧要的。也许这会有所帮助。我在谷歌上看到了一些可供选择的web服务,但我看到的那些不支持AZN。但我没有花太多时间做那件事,那是你的工作。我确实发现了这个:
您可以将其添加到应用程序中,可以通过添加浏览器控件并将其嵌入自定义页面并将结果检索到主窗体中。但最终,你自己回答了这个问题: 否则使用write函数进行转换(我已经编写了) 如果你还没有找到一个解决方案,那就自己动手吧 还可以尝试:
这个简单的算法将在键值对列表中为您提供所需的一切
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;
}
公共静态列表GetCurrencyListFromWeb(out DateTime currencyDate)
{
List returnList=新列表();
string date=string.Empty;
使用(XmlReader xmlr=XmlReader.Create)(@)http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"))
{
xmlr.ReadToFollowing(“多维数据集”);
while(xmlr.Read())
{
如果(xmlr.NodeType!=XmlNodeType.Element)继续;
if(xmlr.GetAttribute(“时间”)!=null)
{
日期=xmlr.GetAttribute(“时间”);
}
else returnList.Add(新的KeyValuePair(xmlr.GetAttribute(“currency”)、decimal.Parse(xmlr.GetAttribute(“rate”)、CultureInfo.InvariantCulture));
}
currencyDate=DateTime.Parse(日期);
}
返回列表。添加(新的KeyValuePair(“EUR”,1));
退货清单;
}
如需更深入的解释,请留言。这段代码不错,但有没有理由不选择常用的词典
?