Copy codeThe Code is as follows: class ExchangeRate
{
Private const string _ BASEURL = "webpage address ";
Public const string CURRENCYCODE = "currency type separated by '|'"; // currency type
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 );
}
}