【问题标题】:Fetch currency rate in C# Winform在 C# Winform 中获取货币汇率
【发布时间】:2017-04-19 16:10:03
【问题描述】:

我的书中有一个听起来很简单的 winform 任务。使用windows窗体。在文本框中获取最新的货币汇率。

1 USD = ??? INR

我认为显示转换后货币的最佳解决方案是使用带有查询字符串的 Process 方法...

http://www.xe.com/ucc/convert.cgi?Amount=" + costTextBox.Text.ToString() + "&From=USD&To=INR"

但是如何获取并分离到文本框中的值呢?

【问题讨论】:

  • 在您指定的页面来源中,它声明“警告:根据使用条款,禁止自动提取费率。”因此,您可以从那里解析值,但显然这是不可取的。
  • 您也可以在 Google 中查询(1 美元 = ? INR),它会出现在结果之前,例如(1 美元 = 55.2211607 印度卢比)
  • hmmmmm 对 @James 我只认为这是我能想到的最好的事情......现在让我弄清楚我应该使用谷歌吗?

标签: c# winforms


【解决方案1】:

与其试图从 xe.com 的响应中删除价值,我建议要么从 here 购买他们的服务,要么使用这个免费的 webservice

  1. 将 wsdl 添加为服务引用。
  2. 创建 SoapClient
  3. 调用 ConversionRate 方法。

var result = client.ConversionRate(CurrencyConverterService.Currency.USD,
                                   CurrencyConverterService.Currency.INR);

【讨论】:

  • 如果这不是我的课本练习练习,我会购买它的 :).......非常感谢,让我检查一下是否应该在我的winform(文本框).....
  • SoapClient 也可以添加到 Winfroms 中???无法使用 PHP,也无法在 C# WinForm 中添加soapcliet。
  • 您是否尝试添加服务参考。右键单击“引用”文件夹并选择“添加服务引用”。在随后打开的对话框中,将 url 添加到 wsdl - webservicex.net/CurrencyConvertor.asmx?WSDL
【解决方案2】:

看看Google Finance API,看看下面的函数:

public static decimal Convert(decimal amount, string from, string to)
        {
            WebClient web = new WebClient();

            string url = string.Format("http://www.google.com/ig/calculator?hl=en&q={0}{1}=?{2}", amount, from.ToUpper(), to.ToUpper());

            string response = web.DownloadString(url);

            Regex regex = new Regex("rhs: \\\"(\\d*.\\d*)");
            Match match = regex.Match(response);

            return System.Convert.ToDecimal(match.Groups[1].Value);
        }

那么你就可以这样使用函数了:

decimal converted = Convert(3.25, "USD", "EUR");

【讨论】:

  • 很好,对可以发出的请求数量有什么限制吗?
  • 对我来说不太容易理解 google api (java),或者我没有得到与 C# 相关的任何内容......
【解决方案3】:

您可以使用Yahoo currency converter

此方法将为您提供当前汇率:

    decimal getCurrencyRate(string currFrom, string currTo)
    {
        decimal result;
        using (WebClient c = new WebClient())
        {
            string data = c.DownloadString(string.Format("http://download.finance.yahoo.com/d/quotes.csv?s={0}{1}=X&f=sl1d1t1ba&e=.csv", currFrom, currTo));
            string rate = data.Split(',')[1];
            var style = NumberStyles.Number;
            var culture = CultureInfo.CreateSpecificCulture("en-US");
            decimal.TryParse(rate, style, culture, out result);
        }
        return result;
    }

而你使用这种方式:

        //convert $50 to INR
        decimal val = 50.0M;
        //get rate
        decimal rate = getCurrencyRate("USD", "INR");
        //calculate value in INR
        decimal inrVal = val * rate;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 2012-01-04
    • 2016-04-27
    • 1970-01-01
    • 2011-02-19
    相关资源
    最近更新 更多