所以我在我的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值来指定每个名称,但这种情况并没有好转。
So I have this subset in my XML string:
<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>It's not required that all of the types of loan identifier values will be in any set. I'm trying to figure out how to pull the LoanIdentifierValue values and associate them with the sibling tag for each one. So far, I've tried this:
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 }) }) });But it's not working. The foreach set just skips and goes on its merry way:
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()); } }I also tried doing it as a smaller set rather than specifying each name by pulling the Name.LocalName value of the child elements of LOAN_IDENTIFIER, but that fared no better.
最满意答案
好简单。 这是嵌套字典
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());Very easy. Here is the nested dictionary
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());更多推荐
发布评论