// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Linq; using System.Xml.Linq; namespace E { public class Program { public static void Main(string[] args) { XElement data = XElement.Parse(@" ^\d{0,3}$ N 3.0 "); // let us first see data Console.WriteLine(data.ToString()); // this finds something because there is a Rule tag under data when itself is included var ruleq = from rules in data.DescendantsAndSelf("{http://schemas.dk/load}Rule") select new { Type = rules.Attribute("type").Value, Priority = rules.Attribute("priority").Value }; foreach(var ruler in ruleq) { Console.WriteLine(ruler.Type + " " + ruler.Priority); } // this finds nothing because there are no Rule tags under data when itself is included // (note that this would work if data was an element outside of the Rule tag) var ruleq2 = from rules in data.Descendants("{http://schemas.dk/load}Rule") select rules; foreach(var ruler2 in ruleq2) { Console.WriteLine(ruler2.ToString()); } // what I think you want var ruleq3 = from rules in data.DescendantsAndSelf("{http://schemas.dk/load}Rule") select new { Type = rules.Attribute("type").Value, Priority = rules.Attribute("priority").Value, RegExp = rules.Element("{http://schemas.dk/load}RegExp").Value, Pattern = rules.Element("{http://schemas.dk/load}Pattern").Value, }; foreach(var ruler3 in ruleq3) { Console.WriteLine(ruler3.Type + " " + ruler3.Priority + " " + ruler3.RegExp + " " + ruler3.Pattern); } } } }