值不能为空。参数名称:元素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了值不能为空。参数名称:元素相关的知识,希望对你有一定的参考价值。

我正在尝试从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>

我不断收到“值不能为空。参数名称:元素”错误。

[当我通过数据进行挖掘时,我看到一些USTrades没有我要搜索的元素-会导致此问题吗?如果是这样,我该如何处理? (即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");

以上是关于值不能为空。参数名称:元素的主要内容,如果未能解决你的问题,请参考以下文章

值不能为空。参数名称:编码器[重复]

反序列化错误:值不能为空。参数名称:类型

值不能为空。参数名称:数据源。编辑数据网格并保存到数据库

实体框架:值不能为空。参数名称:类型

System.ArgumentNullException: '值不能为空。参数名称:输入'Xamarin?

值不能为空。参数名称:httpContext