XElement and Linq

The following code illustrates how to use Linq to query an XElement.

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        var bench = new XElement("Root",
            new XElement("subRoot",
            new XElement("Q", "T")),
            new XComment("comment")
        );
        IEnumerable<string> query = from toolbox in bench.Elements()
                                    where toolbox.Elements().Any(tool => tool.Value == "T")
                                    select toolbox.Value;
        
        foreach(string s in query){
            Console.WriteLine(s);        
        }
    }
}

The output:


T
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.