程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net 獲取銀行貨幣匯率的代碼

asp.net 獲取銀行貨幣匯率的代碼

編輯:ASP.NET基礎
復制代碼 代碼如下:
class ExchangeRate
{

private const string _BASEURL = "網頁地址";
public const string CURRENCYCODE = "貨幣類型以'|'隔開";//貨幣類型

public Hashtable GetValues()
{
Hashtable htReturn = new Hashtable();

string url = _BASEURL; //+ HttpUtility.UrlEncode(tmSet.ToString("yyyy/MM/dd", DateTimeFormatInfo.InvariantInfo));

WebClient wc = new WebClient();
string sHtml = wc.DownloadString(url);
string sXml = string.Empty;
int iValueCnt = CURRENCYCODE.Split(new char[] { '|' }, 10, StringSplitOptions.RemoveEmptyEntries).Length;
string[] sTBody = sHtml.Split(new string[] { "<tbody>", "</tbody>" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string ss in sTBody)
{
if (ss.Contains("Currency Name"))
{
string[] sbrs = ss.Split(new string[] { "</tr>" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string sbr in sbrs)
{
string scur = string.Empty;
double dRate = GetCurrencyRate(sbr, out scur);

if (dRate != 0.0 && !string.IsNullOrEmpty(scur))
{
htReturn.Add(scur, dRate * 0.01);
if (htReturn.Count >= iValueCnt)
break;
}
}
break;
}
}


return htReturn;
}

private double GetCurrencyRate(string source, out string sCurrency)
{
sCurrency = string.Empty;

string sPattern = @"<td.+?>(.+?)</td>";
foreach (Match m in Regex.Matches(source, sPattern))
{
string ss = m.Groups[1].Value;
if (IsNumeric(ss))
return double.Parse(ss);
else
{
if (CURRENCYCODE.Contains(ss))
sCurrency = ss.Trim();
else
break;
}
}

return 0.0;
}

public static bool IsNumeric(string str)
{
if (string.IsNullOrEmpty(str)) return false;

System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^[-]?\d+[.|,]?\d*$");
return reg.IsMatch(str);
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved