LINQ to XML: How to select the next element

后端 未结 4 1328
慢半拍i
慢半拍i 2021-01-04 03:25

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


  
    barcodes
    

        
相关标签:
4条回答
  • 2021-01-04 03:53

    EDIT

    I believe this will get you the element after the jobSteps node:

    XElement elementAfterJobsteps = xml.Descendants("plist").FirstOrDefault().Descendants("jobSteps").FirstOrDefault().ElementsAfterSelf().FirstOrDefault();
    

    END EDIT

    foreach (XElement El in xml.Descendants("plist")) {
            var localResults = 
                from elements in El.Descendants("dict")
                where elements.Value == "jobSteps"
                select elements;
    
           //process localResults 
     }
    

    Or, even simpler, try method syntax

     foreach (XElement El in xml.Descendants("plist")) {
        var localResults = El.Descendants("dict").Where(dict => dict.Value == "jobSteps");
    
        //process localResults 
     }
    
    0 讨论(0)
  • 2021-01-04 03:54
    var q = xml
            .Descendants("plist")
            .Descendants("dict")
            .Where(item => item.Value == "jobSteps")
            .Select(item => item.NextNode)
            .SingleOrDefault() // add this if you expect single match
            ;
    

    The q will be either a single array node or a sequence of array nodes depending whether you use SingleOrDefault().

    0 讨论(0)
  • 2021-01-04 04:01
    var q = (from key in xml.Descendants("key")
                where key.Value == "jobSteps"
                from array in xml.Descendants("array")
                select key.NodesAfterSelf() // In all nodes after <key>jobSteps</key>
                        .OfType<XElement>() // which are elements
                        .Where(element => element.Name == "array") // and name is array,
                        .First() // select first of them
            ).First(); // and select first of whatever is found
    

    NOTE: Above code may throw InvalidOperationException while calling First() if no result is found.

    0 讨论(0)
  • 2021-01-04 04:06

    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());
    
    0 讨论(0)
提交回复
热议问题