asp.net code to obtain bank currency exchange rate

Source: Internet
Author: User
Tags foreach contains empty net return string split
Copy CodeThe code is as follows:
Class ExchangeRate
{

Private Const string _baseurl = "web address";
Public Const string CurrencyCode = "currency type to ' delimited";//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[] {'}, 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);
}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.