C#中的LINQ to XML,让家长当孩子满足条件 [英] C# Linq to XML, get parents when a child satisfy condition

查看:98
本文介绍了C#中的LINQ to XML,让家长当孩子满足条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助。我有这样的XML文档:<?/ p>

 < XML版本=1.0编码=UTF-8> 
< MyItems>
<家长UPC =A00000000000000000000001SKU =archivo =pantalon1.jpg>
<儿童UPC =101SKU =archivo =形象## JPG>
<如GrandChild archivo =形象## JPG/>
< /儿童>
<儿童UPC =102SKU =archivo =形象## JPG>
<如GrandChild archivo =形象## JPG/>
< /儿童>
< /父母与GT;
<家长UPC =A00000000000000000000002SKU =archivo =形象## JPG>
<儿童UPC =101SKU =archivo =形象## JPG>
<如GrandChild archivo =形象## JPG/>
< /儿童>
<儿童UPC =102SKU =archivo =形象## JPG>
<如GrandChild archivo =形象## JPG/>
< /儿童>
< /父母与GT;
<家长UPC =200SKU =archivo =形象## JPG>
<儿童UPC =201SKU =archivo =形象## JPG>
<如GrandChild archivo =形象## JPG/>
< /儿童>
<儿童UPC =202SKU =archivo =形象## JPG>
<如GrandChild archivo =形象## JPG/>
< /儿童>
< /父母与GT;
< / MyItems>



然后,我想选择所有的'家长',其中一个'儿童'fullfil条件。例如,所有的家长至极包含一个孩子在那里,孩子属性UPC等于 101



我在研究这个文章:
基于子节点



但我只是不能得到我想要的东西。



的属性选择节点

感谢,并有一个愉快的一天!


解决方案

 的XDocument文档= ... ; 
VAR targetUpc = 101;
VAR的查询= doc.Descendants(母公司)
。凡(P => p.Elements(孩子)
。任何(C =>(INT)C。属性(UPC)== targetUpc)
);



所以查询是用来做什么选择名为所有子孙元素,其中任何命名子元素的儿童有一个名为的属性 UPC 等于目标UPC值, targetUpc 。希望你应该能够遵循。


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.

这篇关于C#中的LINQ to XML,让家长当孩子满足条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆