Mega Code Archive

 
Categories / C# Book / 05 LINQ XML
 

0548 Retrieving elements

The next example uses a SelectMany query to retrieve the hand tools in all toolboxes: 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("X", "Y"), new XElement("X1", "Y1")), new XElement("E", new XElement("C", "D"), new XElement("A", "B")), new XComment("comment") ); IEnumerable<string> query = from toolbox in bench.Elements() from tool in toolbox.Elements() where tool.Name == "E" select tool.Value; foreach(string s in query){ Console.WriteLine(s); } } }