问题描述
我有以下代码:
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是:
testData is:
<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>
但是,使用此代码和数据,我得到了以下运行时异常:
But I get the following runtime exception with this code and data:
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:
每个xml记录"中都有几个int值(4);有成千上万的记录.在不尝试戴上雷曼帽(不太适合我)的情况下,如何确定导致上溢或下溢的问题值到底是哪个值?
There are several int values (4) in each xml "record"; there are thousands of records. How can I, without trying to put on a Rainman hat (which doesn't fit me very well) determine just which value is the problematic one causing the overflow or underflow?
如果是下溢(msg异常表示"OverflowException"和对于Int32而言值太大或太小",这是否可能是这四个int成员之一的空值引起的?如果是,如何告诉它将空值视为0?
If it's an underflow (the exception msg says both "OverflowException" and "Value was either too large or too small for an Int32", could it be cause by an empty value for one of those four int members? If so, how can I tell it to consider an empty value as 0?
推荐答案
这就是为什么大多数编码器最终都使用扩展方法而不是LINQ的原因.改写为:
This is why most coders end up using extension methods, but not LINQ. Rewrite as:
private static SiteQuery ParseSiteQuery(XElement sitequery)
{
return 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,
};
}
然后做
List<SiteQuery> sitequeries = xmlDoc.Descendants("SiteQuery")
.Select(ParseSiteQuery).ToList();
现在,当发生异常时,您将在此转换函数内深入了解,范围为sitequery
,可立即了解导致故障的特定XElement.
Now when the exception occurs, you'll break inside this conversion function, with sitequery
in scope, giving immediate knowledge of what particular XElement caused the failure.
然后,您可以使用quickwatch表达式快速找出导致该异常的初始化程序.甚至为每个属性分配编写单独的语句.
Then you can use quickwatch expressions to quickly find out what initializer caused the exception. Or even write separate statements for each property assignment.
这篇关于如何确定哪个元素导致溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!