C# 谷歌货币转换器

C# 谷歌货币转换器,c#,regex,decimal,converter,currency,C#,Regex,Decimal,Converter,Currency,我在过去几年中使用过这个代码,但是谷歌似乎已经改变了他们的一些链接。由于某些原因,我收到以下错误消息: “输入字符串的格式不正确。” 在以下行中: decimal rate = System.Convert.ToDecimal(match.Groups[1].Value); 我的代码: try { WebClient web = new WebClient(); string url = string.Format("https://www.google.com/finance

我在过去几年中使用过这个代码,但是谷歌似乎已经改变了他们的一些链接。由于某些原因,我收到以下错误消息:


“输入字符串的格式不正确。”

在以下行中:

decimal rate = System.Convert.ToDecimal(match.Groups[1].Value);
我的代码:

try
{
    WebClient web = new WebClient();
    string url = string.Format("https://www.google.com/finance/converter?a={2}&from={0}&to={1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);

    string response = web.DownloadString(url);

    Regex regex = new Regex("rhs: \\\"(\\d*.\\d*)");
    Match match = regex.Match(response);
    decimal rate = System.Convert.ToDecimal(match.Groups[1].Value);

    return rate;
}
catch
{
    return 0;
}

你可能不喜欢这种方法,但它完成了任务

WebClient web = new WebClient();
string url = string.Format("https://www.google.com/finance/converter?a={2}&from={0}&to={1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);

string response = web.DownloadString(url);

var split  = response.Split((new string[] { "<span class=bld>"}),StringSplitOptions.None);
var value = split[1].Split(' ')[0];
decimal rate = decimal.Parse(value,CultureInfo.InvariantCulture);
WebClient web=newwebclient();
字符串url=string.Format(“https://www.google.com/finance/converter?a={2} &from={0}&to={1}”,fromCurrency.ToUpper(),toCurrency.ToUpper(),amount);
字符串响应=web.DownloadString(url);
var split=response.split((新字符串[]{“}),StringSplitOptions.None);
var值=拆分[1]。拆分(“”)[0];
十进制速率=decimal.Parse(值,CultureInfo.InvariantCulture);

您可能不喜欢这种方法,但它完成了任务

WebClient web = new WebClient();
string url = string.Format("https://www.google.com/finance/converter?a={2}&from={0}&to={1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);

string response = web.DownloadString(url);

var split  = response.Split((new string[] { "<span class=bld>"}),StringSplitOptions.None);
var value = split[1].Split(' ')[0];
decimal rate = decimal.Parse(value,CultureInfo.InvariantCulture);
WebClient web=newwebclient();
字符串url=string.Format(“https://www.google.com/finance/converter?a={2} &from={0}&to={1}”,fromCurrency.ToUpper(),toCurrency.ToUpper(),amount);
字符串响应=web.DownloadString(url);
var split=response.split((新字符串[]{“}),StringSplitOptions.None);
var值=拆分[1]。拆分(“”)[0];
十进制速率=decimal.Parse(值,CultureInfo.InvariantCulture);

输入字符串的格式不正确。-同样的错误:/it起作用了!谢谢…如果我可以问的话,为什么我不应该喜欢这种方法?@MarkFenech一些开发人员并不真的喜欢字符串操作技术来提取数据,但是当它用于这样的东西时,我认为它是理想的。输入字符串的格式不正确同样的错误:/it起作用了!谢谢…如果我可以问一下,为什么我不喜欢这种方法?@MarkFenech一些开发人员并不真的喜欢字符串操作技术来提取数据,但是当它用于类似的东西时,我认为它是理想的