简体   繁体   中英

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

I need some 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'm trying to select all the 'Parents' where a 'Child' fullfil a condition. Example, all the parents wich contains a child where, child attribute upc is equal to 101

I was studying this article: Select nodes based on properties of descendant nodes

But I just cant get what I want.

Thanks and have a nice day!

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 child elements named Child have an attribute named upc that is equal to the target upc value, targetUpc . Hopefully you should be able to follow that.

Use a Where with a nested Any .

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

try this:

            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);

This works nicely 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM