Mega Code Archive

 
Categories / C# Book / 05 LINQ XML
 

0549 Query attribute value

You can also use casts in LINQ queries. using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; class Program { static void Main() { var data = XElement.Parse(@"<data><customer id='1' name='A' credit='100' /><customer id='2' name='B' credit='150' /><customer id='3' name='C' /></data>"); IEnumerable<string> query = from cust in data.Elements() where (int?)cust.Attribute("credit") > 100 select cust.Attribute("name").Value; foreach(string s in query){ Console.WriteLine(s); } } } The output: B