Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to XML: How to select the next element

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>

I need to get the array after jobSteps.

I have this so far:

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

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

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

like image 454
Matt Hudson Avatar asked Feb 25 '11 15:02

Matt Hudson


1 Answers

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());
like image 123
Jon Skeet Avatar answered Sep 20 '22 13:09

Jon Skeet