// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.IO; using System.Xml; namespace XmlProc.XPath { public class Program { private const String XML_FILE = @"C:\work\employees.xml"; public static void Main(string[] args) { // read from file to DOM tree XmlDocument doc = new XmlDocument(); doc.Load(XML_FILE); // iterate over all employee elements foreach(XmlElement employee in doc.SelectNodes("//employee[@no>1 and role!='Intern']")) { // find attribute no string no = employee.SelectSingleNode("@no").Value; // find sub elements name and role string name = employee.SelectSingleNode("name/text()").Value; string role = employee.SelectSingleNode("role/text()").Value; // print Console.WriteLine("no = " + no); Console.WriteLine("name = " + name); Console.WriteLine("role = " + role); } } } }