簡體   English   中英

值不能是 null。 參數名稱:元素

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM