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

时间:2019-10-17 13:33:47

标签: c# xml

我正在尝试从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没有我要搜索的元素-会引起问题吗?如果是这样,我该如何处理? (即,AusTrade中没有ScheduledPaymentAmount元素)

谢谢

2 个答案:

答案 0 :(得分:0)

您可以修改查询以仅搜索具有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");

答案 1 :(得分:0)

问题似乎源于0000值。我最终创建了一个新类,并将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();

Presto,一切都很好。从长远来看,这样做效果更好,因为我最终希望获得更多的价值。