简体   繁体   English

使用LINQ to XML访问值

[英]Accessing values using LINQ to XML

I am working on an XML format that is generated automatically by another application, and I want to traverse the XML using Linq to XML, but I'm not sure how to get the values I need. 我正在研究由另一个应用程序自动生成的XML格式,我想使用Linq to XML遍历XML,但是我不确定如何获取所需的值。

Here is a snippet of the XML: 这是XML的代码段:

<dict>
<key>Major Version</key><integer>1</integer>    
<key>Tracks</key>
<dict>
    <key>3620</key>
    <dict>
        <key>Track ID</key><integer>3620</integer>
        <key>Name</key><string>Ran</string>
        <key>Age</key><integer>22</integer>         
    </dict>
    <key>3622</key>
    <dict>
        <key>Track ID</key><integer>3622</integer>
        <key>Name</key><string>Jardine</string>
        <key>Age</key><integer>24</integer>         
    </dict>

as you can see, dict is repeated internally, and the key and values are separated by individual nodes. 如您所见, dict在内部重复,并且键和值由单个节点分隔。 I need to get dict nodes where their key age is greater than a value (or basically, I need to filter by a value under the dict ) 我需要获取其键age大于值的dict节点(或者基本上,我需要根据dict下的值进行过滤)

Here's what I have got so far to get all Age values as a list: 到目前为止,这是我将所有Age值作为列表获取的结果:

        var ran = x.Root.Elements("dict")
                    .Elements("dict")
                    .Elements("dict")
                    .Select(r =>
                        r.Descendants("key")
                        .Where(w => w.Value == "Age")
                        .Select(s => (XElement)
                            s.NextNode
                            )
                    ).ToList();

Since the Key 's actual value is stored as a sibling node, I access those using NextNode . 由于Key的实际值存储为兄弟节点,因此我使用NextNode访问它们。

However, that's where the problem is. 但是,这就是问题所在。 How do I filter those XElements in the variable ran according to a condition, such as greater than a value? 如何根据条件(例如大于值)过滤变量中ran XElements

I tried ran.Any and ran.Select but I'm not sure which lambda expression to use. 我尝试了ran.Anyran.Select但不确定使用哪个Lambda表达式。 Or should I append my filter in my original ran query? 还是应该在原始ran查询中附加过滤器?

Using ElementsAfterSelf.First() you can move on to the next XElement. 使用ElementsAfterSelf.First() ,可以继续到下一个XElement。 This code is making a lot of assumptions that there will be another XElement and it will be the integer element and the value will be an int. 这段代码大量假设将有另一个XElement,它将是整数元素,而值将是一个int。 You will need to decide how much you trust the other system is always sending you the XML in this format or if you need to write some defensive code. 您将需要确定您对另一个系统始终以这种格式发送XML的信任程度,或者是否需要编写一些防御性代码。

var age = 23;

var ran = xdoc.Root.Elements("dict")
              .Elements("dict")
              .Elements("dict")
              .Elements("key")
              .Where(x => (string) x == "Age" && (int) x.ElementsAfterSelf.First() > age)
              .Select(x=>x.Parent);

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

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