Mega Code Archive
 
 
    
0547 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 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