Reputation: 3903
With the below xml, how can I group the following by str/@itemNo
if and only if arr[@name = 'ATR_IsFamily'] / str = 1
<root>
<doc>
<arr name="ATR_IsFamily">
<str>0</str>
</arr>
<arr name="ATR_VendorId">
<str>I23</str>
</arr>
<str name="itemNo">ABCDEFGHIJ</str>
</doc>
<doc>
<arr name="ATR_IsFamily">
<str>1</str>
</arr>
<arr name="ATR_VendorId">
<str>I23</str>
</arr>
<str name="itemNo">123456789</str>
</doc>
</root>
This is what I have so far:
var searchResults = from rt in element.Descendants("doc").Descendants("str")
where (String)rt.Attribute("name") == "itemNo"
&& rt.Parent.Descendants("arr").ElementAt(1).Value == "1"
group rt by new { d = (String)rt.Value.Substring(0,6) } into grp
select grp;
This condition is where I am stuck:
rt.Parent.Descendants("arr").ElementAt(1).Value == "1"
How do I set this condition?
Upvotes: 2
Views: 141
Reputation: 1242
Couple things:
rt.Parents.Descendants("arr") is going to return you an IEnumerable<XElement> with two items. By using .ElementAt(1), you'd be selecting the second element, which would be the ATR_VendorId. You'd want .ElementAt(0).
Secondly, you're going to want to select the str node beneath that <arr> element, so you'd probably want something like rt.Parents.Descendants("arr").ElementAt(0).Element("str").Value
Upvotes: 2