2

こんにちは、Google Finance API から取得したデータの乗算に問題があります。私が今していることは間違っていることを知っているので、それを行う方法とその例についてのいくつかの指針が欲しい.

    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");

}

エラー エラー

4

2 に答える 2

1

データ属性の値が数値でないか、指定されていない可能性があります。TryParse メソッドを使用するか、try..catch.. ブロックでコードをラップしてください。

 decimal currentPrice;
 if(decimal.TryParse(currentPrice1.Text, out currentPrice))
  {
     //
   }

またはint.TryParse、データ属性の値が整数の場合に使用します。

于 2012-07-19T10:56:21.157 に答える
0

コードcurentPrice1.Textの印刷画面を見ると、nullまたは空、あるいは数値以外のテキストが含まれている可能性があります

だからしてください

int currentPrice = 0;

if(!string.IsEmptyOrNull(currentPrice1.Text))
{
   int price = 0;
   if(int.TryParse(currentPrice1.Text, out price))
   {
       currentPrice = price ;
   }
}
于 2012-07-19T10:59:13.840 に答える