LINQ to XML:如果存在另一个兄弟元素,则为兄弟的值(LINQ to XML: value of sibling if another sibling element exists)

编程入门 行业动态 更新时间:2024-10-21 10:31:10
LINQ to XML:如果存在另一个兄弟元素,则为兄弟的值(LINQ to XML: value of sibling if another sibling element exists)

所以我在我的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());

更多推荐

本文发布于:2023-07-17 07:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:兄弟   则为   元素   XML   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!