[ACCEPTED]-How to select a specific node with LINQ-to-XML-linq-to-xml

Accepted answer
Score: 49

Assuming the ID is unique:

var result = xmldoc.Element("Customers")
                   .Elements("Customer")
                   .Single(x => (int?)x.Attribute("ID") == 2);

You could also 2 use First, FirstOrDefault, SingleOrDefault or Where, instead of Single for different 1 circumstances.

Score: 4

I'd use something like:

dim customer = (from c in xmldoc...<Customer> 
                where c.<ID>.Value=22 
                select c).SingleOrDefault 

Edit:

missed the c# tag, sorry......the 1 example is in VB.NET

More Related questions