C # Linq to XML, get parents when a child satisfies a condition

I need help. I have this XML document:

<?xml version="1.0" encoding="utf-8"?> <MyItems> <Parent upc="A00000000000000000000001" sku="" archivo="pantalon1.jpg"> <Child upc="101" sku="" archivo="image##.jpg"> <GrandChild archivo="image##.jpg" /> </Child> <Child upc="102" sku="" archivo="image##.jpg"> <GrandChild archivo="image##.jpg" /> </Child> </Parent> <Parent upc="A00000000000000000000002" sku="" archivo="image##.jpg"> <Child upc="101" sku="" archivo="image##.jpg"> <GrandChild archivo="image##.jpg" /> </Child> <Child upc="102" sku="" archivo="image##.jpg"> <GrandChild archivo="image##.jpg" /> </Child> </Parent> <Parent upc="200" sku="" archivo="image##.jpg"> <Child upc="201" sku="" archivo="image##.jpg"> <GrandChild archivo="image##.jpg" /> </Child> <Child upc="202" sku="" archivo="image##.jpg"> <GrandChild archivo="image##.jpg" /> </Child> </Parent> </MyItems> 

Then I try to select all the "Parents" where "Child" fills the condition. For example, all parents who have a child where the upc child attribute is 101

I studied this article: Select nodes based on properties of descendant nodes

But I just can't get what I want.

Thank you and have a nice day!

+6
source share
4 answers
 XDocument doc = ...; var targetUpc = 101; var query = doc.Descendants("Parent") .Where(p => p.Elements("Child") .Any(c => (int)c.Attribute("upc") == targetUpc) ); 

So, what the query does is select all descendant elements named Parent , where any of its children elements named Child has an attribute named upc that is equal to the target value of upc, targetUpc . Hope you can follow this.

+8
source

Use Where with nested Any .

 var xml = XElement.Parse(yourString); var result = xml.Elements("Parent").Where(parent => parent.Elements("Child").Any(child => child.Attribute("upc").Value == "101")); 
+1
source

try the following:

  string xml = @"<?xml version=""1.0"" encoding=""utf-8""?> <MyItems> <Parent upc=""A00000000000000000000001"" sku="""" archivo=""pantalon1.jpg""> <Child upc=""101"" sku="""" archivo=""image##.jpg""> <GrandChild archivo=""image##.jpg"" /> </Child> <Child upc=""102"" sku="""" archivo=""image##.jpg""> <GrandChild archivo=""image##.jpg"" /> </Child> </Parent> <Parent upc=""A00000000000000000000002"" sku="""" archivo=""image##.jpg""> <Child upc=""101"" sku="""" archivo=""image##.jpg""> <GrandChild archivo=""image##.jpg"" /> </Child> <Child upc=""102"" sku="""" archivo=""image##.jpg""> <GrandChild archivo=""image##.jpg"" /> </Child> </Parent> <Parent upc=""200"" sku="""" archivo=""image##.jpg""> <Child upc=""201"" sku="""" archivo=""image##.jpg""> <GrandChild archivo=""image##.jpg"" /> </Child> <Child upc=""202"" sku="""" archivo=""image##.jpg""> <GrandChild archivo=""image##.jpg"" /> </Child> </Parent> </MyItems>"; XElement MyItems = XElement.Parse(xml); var parents = MyItems.Elements("Parent").Where(parent => parent.Elements("Child").Any(child => child.Attribute("upc").Value == "101")); foreach (var parent in parents) Console.WriteLine(parent.Attribute("upc").Value); 
+1
source

This works well for me:

 var query = from p in XDocument.Parse(xml).Root.Elements("Parent") where ( from c in p.Elements("Child") where c.Attribute("upc").Value == "101" select c ).Any() select p; 
+1
source

Source: https://habr.com/ru/post/898115/


All Articles