C# 如何比较多个级别上的Decentant

C# 如何比较多个级别上的Decentant,c#,visual-studio,attributes,linq-to-xml,C#,Visual Studio,Attributes,Linq To Xml,我目前正在处理XML文件,其中必须检索特定元素下的节点。这些节点大多包含DateTime对象,当检索到它们时,它们会得到更新。问题是其中一个属性比其他属性低两个级别(导致需要第二个.subjects)。如何将两个级别更低的属性(\u StartDate)与一个级别不更低的属性进行比较。我特别希望将\u StartDate与\u accountreportedate进行比较 var creditLiabilities = xDoc.Descendants("CREDIT_LIABILITY").D

我目前正在处理XML文件,其中必须检索特定元素下的节点。这些节点大多包含
DateTime
对象,当检索到它们时,它们会得到更新。问题是其中一个属性比其他属性低两个级别(导致需要第二个
.subjects
)。如何将两个级别更低的属性(
\u StartDate
)与一个级别不更低的属性进行比较。我特别希望将
\u StartDate
\u accountreportedate
进行比较

var creditLiabilities = xDoc.Descendants("CREDIT_LIABILITY").Descendants("_PAYMENT_PATTERN")
                .Select(x => new
                    {
                        Element = x,
                        AccountOpenedDate = x.Attribute("_AccountOpenedDate") == null ? string.Empty : x.Attribute("_AccountOpenedDate").Value + "-01",
                        AccountClosedDate = x.Attribute("_AccountClosedDate") == null ? string.Empty : x.Attribute("_AccountClosedDate").Value + "-01",
                        AccountPaidDate = x.Attribute("_AccountPaidDate") == null ? string.Empty : x.Attribute("_AccountPaidDate").Value + "-01",
                        AccountReportedDate = x.Attribute("_AccountReportedDate") == null ? string.Empty : x.Attribute("_AccountReportedDate").Value + "-01",
                        LastActivityDate = x.Attribute("_LastActivityDate") == null ? string.Empty : x.Attribute("_LastActivityDate").Value + "-01",
                        AccountStatusDate = x.Attribute("_AccountStatusDate") == null ? string.Empty : x.Attribute("_AccountStatusDate").Value + "-01",
                        Monthcount = x.Attribute("_MonthsReviewedCount") == null ? string.Empty : x.Attribute("_MonthsReviewedCount").Value,
                        StartDate = x.Attribute("_StartDate") == null ? string.Empty :x.Attribute("_StartDate").Value,
                    }
               ); 
以下是XML代码:

<CREDIT_LIABILITY CreditLiabilityID="CreditLiabilityID_52885" x_MasterTradelineId="52885" x_CDOTradelineId="153216" BorrowerID="Borrower" CreditFileID="CreditFileId_14462 CreditFileId_14460 CreditFileId_14461" x_TradelineGroupId="152665" x_Primary="Y" x_BureauName="Experian" x_SubscriberCode="1631440" _AccountIdentifier="7634134580" _AccountOpenedDate="2014-04" _AccountOwnershipType="Individual" _AccountReportedDate="2014-11" _AccountStatusDate="2015-01" _AccountStatusType="Open" x_FannieAccountStatusType="Open" x_StatusLong="Current account/was 30 days past due date" _AccountType="Installment" _ConsumerDisputeIndicator="N" _DerogatoryDataIndicator="N" _HighBalanceAmount="21811.00" _LastActivityDate="2014-09" _ManualUpdateIndicator="N" x_ActualPaymentAmount="363.00" _MonthsReviewedCount="8" _TermsMonthsCount="60" _UnpaidBalanceAmount="19993.00" x_UnpaidBalanceNumber="19993.0000" CreditLoanType="CreditCard" x_FNMACreditLoanCode="CC" x_RealEstateRelated="0" x_PrimaryRealEstate="0" x_ECOA2="B" x_CREDITOR_Name="FORD CRED" x_PrimaryGroupBy="Installment" _MonthlyPaymentAmount="363.00" _TermsSourceType="Provided" x_EstPayment="N" x_EstPaymentPct="0.020" _MonthsRemainingCount="53">
      <_CREDITOR _Name="FORD CRED" />
      <_CURRENT_RATING _Code="C" _Type="Late30Days" />
      <_LATE_COUNT _30Days="1" _60Days="0" _90Days="0" x_30Lates="07/14" x_60Lates="" x_90Lates="" />
      <_PAYMENT_PATTERN _Data="CCC1CC" _StartDate="2014-11" />
      <CREDIT_REPOSITORY _SourceType="TransUnion" x_Primary="No" _SubscriberCode="03796761" x_CDOTradelineId="153213" />
      <CREDIT_REPOSITORY _SourceType="Equifax" x_Primary="No" _SubscriberCode="644FA04640" x_CDOTradelineId="153215" />
      <CREDIT_REPOSITORY _SourceType="Experian" x_Primary="Yes" _SubscriberCode="1631440" x_CDOTradelineId="153216" />
    </CREDIT_LIABILITY>


您要查找的大多数属性都不在
中,而且由于您只是创建了一个匿名类型的实例,所以根本不清楚您要查找的是哪种类型的比较。我认为最好在
信贷负债
中为
\u accountreportedate
属性设置一个属性,在
\u支付模式中为
\u StartDate
属性设置一个属性。顺便说一句,现在还不清楚降低两个级别意味着什么——元素是一个直接的子元素(如果你能在这里更改XML格式,我会这样做。所有这些下划线都很可怕……),我想比较一下日期。如果_StartDate不等于_AccountReportedDate,它将把值设置为_AccountReportedDateDate。当存在多重信用负债时,这种想法也会起作用。至于格式,我不允许更改任何内容。如果您希望为每个
信用责任
创建一个对象,那就可以了。但我强烈建议不要在预测中进行比较。相反,用一种简单的方法创建一个包含你感兴趣的两个属性的对象,然后分别对这些属性值进行比较?var paymentpatterns=xDoc.substands(“信用负债”).substants和self(“支付模式”).Select(x=>new{Element=x,AccountReportedDate=x.Attribute(“\u AccountReportedDate”)==null?字符串。空:x.Attribute(“\u accountreportedate”).Value,StartDate=x.Attribute(“\u StartDate”)==null?字符串。空:x.Attribute(“\u StartDate”).Value});我并不是只为那两个人建议一个。我只是说你应该把这两个属性作为匿名类型的一部分。