【问题标题】:Value cannot be null. Parameter name: element值不能为空。参数名称:元素
【发布时间】:2019-10-17 23:22:42
【问题描述】:

我正在尝试从 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 - 这会导致问题吗?如果是这样,我该如何解释? (IE A UsTrade 没有 ScheduledPaymentAmount 元素)

谢谢

【问题讨论】:

  • 真的取决于你想要的输出 - 我假设你的例子很简单......xmlStr.Descendants("ScheduledPaymentAmount").where( //something )怎么样。改编自this answer。也许你需要给Descendants一个想要的xml节点列表。
  • 我要做的是对 ScheduledPaymentAmount 求和,但仅当余额大于 0 时。

标签: c# xml


【解决方案1】:

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

【讨论】:

  • var trades = from x in xmlStr.Descendants("USTrade") where x.Element("BalanceAmount") != null & x.Element("ScheduledPaymentAmount") != null & (int) x.Element("BalanceAmount") > 0 选择 (int) x.Element("ScheduledPaymentAmount");是我从你的例子中得到的。它可以在没有 "& (int) x.Element("BalanceAmount") > 0" 的情况下工作,但是一旦我添加该行,我就会收到 Value Null 错误。
【解决方案2】:

问题似乎源于 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,一切都好。从长远来看,这会更好,因为我希望最终获得更多的价值。

【讨论】:

    猜你喜欢
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 2014-05-17
    • 2016-05-14
    相关资源
    最近更新 更多