Mega Code Archive

 
Categories / C# Tutorial / XML LINQ
 

Use Linq to read xml file and query attribute value

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Text; using System.Xml.Linq; public class MainClass {     public static void Main()     {         XElement root = null;         root = XElement.Load(@"\employees.xml");         var result = from item in root.Elements("employee")                      where item.Attributes("employeeid").Count() > 0                      select item.Attribute("employeeid").Value;         foreach (var obj in result)         {             Console.WriteLine(obj);         }          } }