Mega Code Archive

 
Categories / C# Book / 05 LINQ XML
 

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<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