我有這樣的代碼:如何確定哪個元素導致溢出?
String testData = File.ReadAllText("siteQueryTest.txt");
XDocument xmlDoc = XDocument.Parse(testData);
List<SiteQuery> sitequeries =
(from sitequery in xmlDoc.Descendants("SiteQuery")
select new SiteQuery
{
Id = Convert.ToInt32(sitequery.Element("Id").Value),
UPCPackSize = Convert.ToInt32(sitequery.Element("UPCPackSize").Value),
UPC_Code = sitequery.Element("UPC_Code").Value,
crvId = sitequery.Element("crvId").Value,
dept = Convert.ToInt32(sitequery.Element("dept").Value),
description = sitequery.Element("description").Value,
openQty = Convert.ToDouble(sitequery.Element("openQty").Value),
packSize = Convert.ToInt32(sitequery.Element("packSize").Value),
subDept = Convert.ToInt32(sitequery.Element("subDept").Value),
unitCost = Convert.ToDecimal(sitequery.Element("unitCost").Value),
unitList = Convert.ToDecimal(sitequery.Element("unitList").Value),
vendorId = sitequery.Element("vendorId").Value,
vendorItem = sitequery.Element("vendorItem").Value,
}).ToList<SiteQuery>();
TESTDATA是:
<SiteQueries><SiteQuery><Id>00006000002</Id><UPCPackSize>1</UPCPackSize><UPC_Code>00006000002</UPC_Code><crvId></crvId><dept>8</dept><description>ZZ</description><openQty>0.0</openQty><packSize>1</packSize><subDept>80</subDept><unitCost>1.25</unitCost><unitList>5.0</unitList><vendorId>CONFLICT</vendorId><vendorItem>123456</vendorItem></SiteQuery>
. . . // gazillions of other SiteQuery "records"
<SiteQuery><Id>5705654</Id><UPCPackSize>1</UPCPackSize><UPC_Code>5705654</UPC_Code><crvId></crvId><dept>2</dept><description>what do you want</description><openQty>0.0</openQty><packSize>1</packSize><subDept>0</subDept><unitCost>0.55</unitCost><unitList>1.62</unitList><vendorId></vendorId><vendorItem></vendorItem></SiteQuery></SiteQueries>
,但我得到這個代碼和數據以下運行時異常:
System.OverflowException was unhandled
_HResult=-2146233066
_message=Value was either too large or too small for an Int32.
HResult=-2146233066
IsTransient=false
Message=Value was either too large or too small for an Int32.
Source=mscorlib
StackTrace:
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Convert.ToInt32(String value)
at Sandbox.Form1.<button56_Click>b__e(XElement sitequery) in c:\HoldingTank\Sandbox\Form1.cs:line 2041
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
. . .
InnerException:
有幾個INT值(4)在每個xml「記錄」中;有成千上萬的記錄。我怎麼能在沒有嘗試戴上一個Rainman帽子(它不適合我)的情況下確定導致溢出或下溢的問題是哪個值?
如果是下溢(msg表示異常「OverflowException」和「Value對於Int32來說太大或太小」,是否可能是由於這四個int成員之一的值爲空值?如果是這樣我怎麼能告訴它要考慮的一個空值0
捕獲異常,間歇中的處理程序,檢查VAR和陸侃? –
爲什麼不使用小數而不是int? – DrKoch
'它說:'c:\ HoldingTank \ Sandbox \ Form1.cs:2041行'哪一行是? – jessehouwing