Mega Code Archive

 
Categories / C# Book / 05 LINQ XML
 

0532 Element and Attribute

The following code uses foreach loop to list elements. using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; class Program { static void Main() { string xml = @"<Root><client enabled='false'><timeout>60</timeout></client></Root>"; XElement config = XElement.Parse(xml); foreach (XElement child in config.Elements()) Console.WriteLine(child.Name); } } The output: client