LINQ到XML:如何选择下一个元素 [英] LINQ to XML: How to select the next element

查看:109
本文介绍了LINQ到XML:如何选择下一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPhone应用程序plist文件中。它看起来像下面这样:

I have a plist file from an iPhone app. It looks like this below:

<plist version="1.0">
  <dict>
    <key>barcodes</key>
    <array>
      <string>JF893J89FJ-66666</string>
      <string>JF893J89FJ-55555</string>
    </array>
    <key>currentStep</key>
    <integer>1</integer>
    <key>dateFinished</key>
    <date>2010-05-10T18:33:25Z</date>
    <key>dateStarted</key>
    <date>2010-05-10T18:33:25Z</date>
    <key>description</key>
    <string>TEST</string>
    <key>geoRequired</key>
    <string>N</string>
    <key>inProgress</key>
    <string>N</string>
    <key>jobID</key>
    <integer>10085</integer>
    <key>jobSteps</key>
    <array>
      <dict>
        <key>label</key>
        <string>TEST</string>
        <key>response</key>
        <string>matt hudson</string>
        <key>stepID</key>
        <integer>1103</integer>
        <key>typeID</key>
        <integer>4</integer>
      </dict>
    </array>
  </dict>
</plist>



我需要得到jobSteps后的数组。

I need to get the array after jobSteps.

我有这个至今:

XDocument xml = XDocument.Load(rri.Response);

var q = from elements in xml.Descendants("plist").Descendants("dict")
        where elements.Value == "jobSteps"
        select elements;



但是我需要有jobSteps其中的元素之后,以获得下一个项目。

But I need to get the next item after the element that has jobSteps in it.

推荐答案

这并不完全清楚,我亚当的解决方案是否是你想要的,但如果没有,你可能想要看的NextNode 属性:

It's not entirely clear to me whether Adam's solution is what you want, but if not, you might want to look at the NextNode property:

获取此节点的下一个同级节点

Gets the next sibling node of this node.

例如,这个输出数组元素:

For instance, this prints the array element:

using System;
using System.Linq;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        XDocument doc = XDocument.Load("test.xml");
        foreach (var element in doc.Descendants("key")
                                   .Where(x => (string) x == "jobSteps"))
        {
            Console.WriteLine(element.NextNode);
        }
    }
}

请注意,它的跳过空白元素之间 - 但如果有的是与阵列之间的所有文本,这是行不通的 - 所以你想要的:

Note that it's skipping the whitespace between elements - but if there were any text between that and the array, it wouldn't work - so you'd want:

Console.WriteLine(element.NodesAfterSelf().OfType<XElement>().First());

这篇关于LINQ到XML:如何选择下一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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