LINQ to XML:兄弟的价值,如果存在另一个兄弟元素

时间:2017-04-28 17:11:19

标签: c# xml linq

所以我在XML字符串中有这个子集:

<LOANS>
  <LOAN SequenceNumber="1">
    <LOAN_IDENTIFIERS>
      <LOAN_IDENTIFIER>
        <InvestorLoanIdentifier />
        <LoanIdentifierValue>1234567</LoanIdentifierValue>
      </LOAN_IDENTIFIER>
      <LOAN_IDENTIFIER>
        <SellerLoanIdentifer />
        <LoanIdentifierValue>98765432</LoanIdentifierValue>
      </LOAN_IDENTIFIER>
      <LOAN_IDENTIFIER>
        <MERSMinLoanIdentifer />
        <LoanIdentifierValue>ABCDefgHIJK</LoanIdentifierValue>
      </LOAN_IDENTIFIER>
    </LOAN_IDENTIFIERS>
  </LOAN>
</LOANS>

并不要求所有类型的贷款标识符值都在任何集合中。我试图找出如何拉出LoanIdentifierValue值并将它们与每个值的兄弟标记相关联。到目前为止,我已经尝试过了:

XmlNamespace ns = "http://www.mismo.org/residential/2009/schemas_v1_4_2";
var idvals = (from idnumset in rvx.Descendants("LOAN")
             let loannumtype = idnumset.Elements(ns + "LOAN_IDENTIFIERS").Elements(ns + "LOAN_IDENTIFIER")
             let loannumtag = idnumset.Elements(ns + "LOAN_IDENTIFIERS").Elements(ns + "LOAN_IDENTIFIER").Elements(ns + "LoanIdentifierValue")

         select new
         {
             seqnum = (int)int.Parse(idnumset.Attribute("SequenceNumber").Value.ToString())
             ,idset = (from breakoutset in loannumtype.Elements(ns + "InvestorLoanIdentifier") select new { idtype = (string)breakoutset.Name.LocalName, idval = (from breakout2set in loannumtype.Elements(ns + "LoanIdentifierValue") select new { val = (string)breakout2set.Value.ToString() ?? null }) })
             ,icid = (from breakoutset in loannumtype.Elements(ns + "InvestorCommitmentIdentifier") select new { idtype = (string)breakoutset.Name.LocalName ?? "NotApplicable", idval = (from breakout2set in loannumtype.Elements(ns + "LoanIdentifierValue") select new { val = (string)breakout2set.Value.ToString() ?? null }) })
             ,slid = (from breakoutset in loannumtype.Elements(ns + "SellerLoanIdentifier") select new { idtype = (string)breakoutset.Name.LocalName ?? "NotApplicable", idval = (from breakout2set in loannumtype.Elements(ns + "LoanIdentifierValue") select new { val = (string)breakout2set.Value.ToString() ?? null }) })
             ,mmid = (from breakoutset in loannumtype.Elements(ns + "MERS_MINIdentifier") select new { idtype = (string)breakoutset.Name.LocalName ?? "NotApplicable", idval = (from breakout2set in loannumtype.Elements(ns + "LoanIdentifierValue") select new { val = (string)breakout2set.Value.ToString() ?? null }) })
             ,svcid = (from breakoutset in loannumtype.Elements(ns + "ServicerLoanIdentifier") select new { idtype = (string)breakoutset.Name.LocalName ?? "NotApplicable", idval = (from breakout2set in loannumtype.Elements(ns + "LoanIdentifierValue") select new { val = (string)breakout2set.Value.ToString() ?? null }) })
          });

但它不起作用。 foreach套装只是跳过并顺利进行:

foreach (var ids in idvals)
{
   seqnum = ids.seqnum;
   foreach (var idv in ids.idset)
   {
      string idtype = idv.idtype;
      if (idtype != "NotApplicable")
         ilia.Add(seqnum, idv.idval.ToString());
   }
   foreach (var sli in ids.slid)
   {
      string slidtype = sli.idtype;
      if (slidtype != "NotApplicable")
         slaa.Add(seqnum, sli.idval.ToString());
   }
   foreach (var mmli in ids.mmid)
   {
     string mmidtype = mmli.idtype;
     if (mmidtype != "NotApplicable")
       mma.Add(seqnum, mmli.idval.ToString());
   }
   foreach (var svcli in ids.svcid)
   {
     string svctype = svcli.idtype;
     if (svctype != "NotApplicable")
        slia.Add(seqnum, svcli.idval.ToString());
   }
   foreach (var icili in ids.icid)
   {
     string ictype = icili.idtype;
     if (ictype != "NotApplicable")
        icia.Add(seqnum, icili.idval.ToString());
   }
}

我还尝试将其作为较小的集合而不是通过拉动LOAN_IDENTIFIER的子元素的Name.LocalName值来指定每个名称,但这种情况并没有好转。

1 个答案:

答案 0 :(得分:1)

很简单。这是嵌套字典

           XDocument doc = XDocument.Load(FILENAME);

            var dict = doc.Descendants("LOAN").GroupBy(l => (string)l.Attribute("SequenceNumber"),
                    m => m.Descendants("LOAN_IDENTIFIER")
                      .GroupBy(x => x.Elements().FirstOrDefault().Name.LocalName, y => (string)y.Elements().Skip(1).FirstOrDefault())
                      .ToDictionary(x => x.Key, y => y.FirstOrDefault()))
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());