简体   繁体   English

C#Linq to XML,当孩子满足条件时得到父母

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

I need some help. 我需要协助。 I have this xml document: 我有这个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>

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 例如,所有父母都包含一个孩子,其中,子属性upc等于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 . 所以查询的作用是选择名为Parent所有后代元素,其中名为Child任何子元素都具有名为upc的属性,该属性等于目标upc值targetUpc Hopefully you should be able to follow that. 希望你能够遵循这一点。

Use a Where with a nested Any . 使用嵌套AnyWhere

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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