2014-02-19 2 views
0
내가 통화를 변환 구글의 금융 변환기을 사용하고

를 사용하는 경우 "입력 문자열이 올바른 형식이 아니었다"얻기하지만 오류 받고 있어요 :구글의 금융 계산기

input string was not in a correct format

여기 내 코드의를 :

Protected Sub btnGoogleApi_Click(sender As Object, e As EventArgs) 
    Dim amount As Decimal = 0 
    Dim fromCurrency As String 
    Dim toCurrency As String 
    amount = Decimal.Parse(txtAmount.Text.Trim(), System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture) 
    fromCurrency = ddlFrom.SelectedItem.Value 
    toCurrency = ddlTo.SelectedItem.Value 
    If amount <> 0 Then 
     Dim web As New WebClient() 
     Dim url As String = String.Format("http://www.google.com/finance/converter?a={0}&from={1}&to={2}", amount, fromCurrency.ToUpper(), toCurrency.ToUpper()) 
     Dim response As String = web.DownloadString(url) 
     Dim regex As New Regex("rhs: \""(\d*.\d*)") 
     Dim rate As Decimal = System.Convert.ToDecimal(regex.Match(response).Groups(1).Value) 
     lblMessage.Text = ("Real-Time Rate: 1 " + ddlFrom.SelectedItem.Value & " = " & rate & " ") + ddlTo.SelectedItem.Value 
    End If 
End Sub 
+0

디버깅을하셨습니까? 어떤 라인이 끊어 질까요? –

+0

관련없는 참고 사항, 적어도 매우 많은 양이나 매우 적은 금액을 처리하는 경우 통화 값에 소수를 사용하지 않아야합니다. – user2366842

+0

디버거에서 실행 중입니다. 정규식에 맞기 직전의 응답 값은 무엇입니까? – user2366842

답변

0

은이 줄에 해당 오류를 얻고있다

Dim rate As Decimal = System.Convert.ToDecimal(regex.Match(response).Groups(1).Value) 

정규식이 아무 것도 아니므로 빈 문자열을 소수로 변환 할 수 없습니다.

사용중인 Google Finance Converter가 이전 iGoogle API와 동일한 응답을 반환하지 않습니다. 대신 HTML 페이지 문자열을 반환합니다. 나는 당신이 또 다른 api을 사용하여 출력을 더 쉽게 읽을 것을 제안합니다.

Dim regex As New Regex("<span class=bld>(\d*.\d*)") 

변환 속도는 클래스 BLD가있는 HTML span 태그에 있습니다

하지 않으면, 당신이에 정규 표현식있어 변경합니다. 내 정규 표현식은 훌륭하지 않지만 그렇게해야합니다.

출력이 변경되면 그에 따라 조정해야합니다.