[英]Value cannot be null. Parameter name: element
我正在尝试从 XML 信用报告中提取数据。 我需要清除某些条目的能力。
这是我用来尝试获取值列表的代码。
var trades = from x in xmlStr.Descendants("USTrade")
where (int) x.Element("BalanceAmount") > 0
select (int)x.Element("ScheduledPaymentAmount");
示例数据如下所示:
<USTrade>
<ExpandedDataDimensionsReturned code="T" description="Enhanced Trade with All 6.0 Trade Fields" />
<CreditorId>
<CustomerNumber>801ON00630</CustomerNumber>
<Name>BK OF AMER</Name>
<Industry code="ON" description="National Credit Card Cos." />
</CreditorId>
<DateReported format="MM/CCYY">07/2019</DateReported>
<DateOpened format="MM/CCYY">12/2007</DateOpened>
<AccountNumber>4509128960678</AccountNumber>
<HighCreditAmount>0020500</HighCreditAmount>
<BalanceAmount>0000076</BalanceAmount>
<PortfolioType code="R" description="Revolving or Option (open-end account)" />
<Status code="1" description="Pays Acct As Agreed" />
<MonthsReviewed>21</MonthsReviewed>
<AccountDesignator code="I" description="Individual" />
<DateOfLastActivity format="MM/CCYY">07/2019</DateOfLastActivity>
<ScheduledPaymentAmount>0000015</ScheduledPaymentAmount>
<UpdateIndicator code="*" description="Tape" />
<Narratives>
<Narrative code="FE" description="Credit Card" />
<Narrative code="AZ" description="Amount In H/C Column Is Credit Limit" />
</Narratives>
<ExpandedCreditorId>
<Name>BANK OF AMERICA</Name>
</ExpandedCreditorId>
<ExpandedDateReported format="MM/CCYY">07/2019</ExpandedDateReported>
<ExpandedDateOpened format="MM/CCYY">12/2007</ExpandedDateOpened>
<ExpandedCreditLimit>000020500</ExpandedCreditLimit>
<ExpandedBalanceAmount>000000076</ExpandedBalanceAmount>
<ExpandedPortfolioType code="R" description="Revolving or Option (open-end account)" />
<ExpandedStatus code="1" description="Pays Acct As Agreed" />
<ExpandedNarratives>
<Narrative code="FE" description="Credit Card" />
</ExpandedNarratives>
<ExpandedScheduledPaymentAmount>000000015</ExpandedScheduledPaymentAmount>
</USTrade>
<USTrade>
<ExpandedDataDimensionsReturned code="T" description="Enhanced Trade with All 6.0 Trade Fields" />
<CreditorId>
<CustomerNumber>801ON00630</CustomerNumber>
<Name>BK OF AMER</Name>
<Industry code="ON" description="National Credit Card Cos." />
</CreditorId>
<DateReported format="MM/CCYY">10/2018</DateReported>
<DateOpened format="MM/CCYY">12/2007</DateOpened>
<AccountNumber>4509128960565</AccountNumber>
<Status code="B" description="Lost or Stolen Card" />
<DateOfLastActivity format="MM/CCYY">10/2018</DateOfLastActivity>
<UpdateIndicator code="*" description="Tape" />
<Narratives>
<Narrative code="FE" description="Credit Card" />
</Narratives>
<ExpandedCreditorId>
<Name>BANK OF AMERICA</Name>
</ExpandedCreditorId>
<ExpandedDateReported format="MM/CCYY">10/2018</ExpandedDateReported>
<ExpandedDateOpened format="MM/CCYY">12/2007</ExpandedDateOpened>
<ExpandedStatus code="B" description="Lost or Stolen Card" />
<ExpandedNarratives>
<Narrative code="FE" description="Credit Card" />
</ExpandedNarratives>
</USTrade>
我不断收到“值不能是 null。参数名称:元素”错误。
当我通过数据挖掘时,我看到一些没有我要搜索的元素的 USTrades - 这会导致问题吗? 如果是这样,我该如何解释? (IE A UsTrade 没有 ScheduledPaymentAmount 元素)
谢谢
您可以修改查询以仅搜索具有ScheduledPaymentAmount
元素的对象。 例如
var trades = from x in xmlStr.Descendants("USTrade")
where (int) x.Element("BalanceAmount") > 0
and x.Element("ScheduledPaymentAmount") != null
select (int)x.Element("ScheduledPaymentAmount");
问题似乎源于 0000 值。 我最终创建了一个新的 class 并添加了 BalanceAmount 和 ScheduledPaymentAmount 作为属性。 然后我做了:
var trades = (from x in xmlStr.Descendants("USTrade")
where x.Element("BalanceAmount") != null
& x.Element("ScheduledPaymentAmount") != null
select new USTrade
{
BalanceAmount = (int) x.Element("BalanceAmount") ,
ScheduledPaymentAmount = (int)x.Element("ScheduledPaymentAmount")
}).ToList();
monthlySum = (from y in trades
where y.BalanceAmount > 0
select y.ScheduledPaymentAmount).Sum();
很快,一切都好。 从长远来看,这会更好,因为我最终希望获得更多的价值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.