2012-07-19 32 views
2

您好,我無法乘上從google finance api檢索到的數據。我知道我現在正在做的事情是錯誤的,所以我想就如何去做這件事以及一些例子進行說明。在對xml中的數據執行乘法操作時遇到問題

string url = "http://www.google.com/ig/api?stock=" + Server.UrlEncode(symbol1.Text); 
    XmlDocument xdoc = new XmlDocument(); 
    xdoc.Load(url); 
    currentPrice1.Text = GetData(xdoc, "last"); 
    int quantity = 50; 
    int currentPrice = int.Parse(currentPrice1.Text); 
    int totalValue = quantity * currentPrice; 

GetData方法

private string GetData(XmlDocument doc2, string strElement) 
{ 

    XmlNodeList xnodelist5 = doc2.GetElementsByTagName(strElement); 
    XmlNode xnode5 = xnodelist5.Item(0); 
    return Get_Attribute_Value(xnode5, "data"); 

} 

ERROR ERROR

+0

請告訴我們錯誤,當你做乘法 – HatSoft 2012-07-19 10:47:47

+0

燁,我補充說謝謝你提醒我! – Challenger 2012-07-19 10:53:11

回答

1

可能的值數據屬性爲非數值或沒有指定。使用TryParse方法或用try..catch ...塊封裝你的代碼。如果數據屬性的值是整數,則使用int.TryParse

+0

謝謝你,你是一個拯救生命的人。數據屬性是十進制的。 – Challenger 2012-07-19 11:09:49

0

望着具有空或空或一些非數字代碼curentPrice1.Text力量的打印屏幕

因此,請

int currentPrice = 0; 

if(!string.IsEmptyOrNull(currentPrice1.Text)) 
{ 
    int price = 0; 
    if(int.TryParse(currentPrice1.Text, out price)) 
    { 
     currentPrice = price ; 
    } 
} 
相關問題