CSharp examples for XML:XML LINQ
Query XML with LINQ
using System;//w w w. j a v a 2 s . c o m using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; class MainClass { static void Main(string[] args) { XElement rootElement = XElement.Load(@"store.xml"); // select the name of elements who have a category ID of 16 IEnumerable<string> catEnum = from elem in rootElement.Elements() where (elem.Name == "Products" && ((string)elem.Element("CategoryID")) == "16") select ((string)elem.Element("ModelName")); foreach (string stringVal in catEnum) { Console.WriteLine("Category 16 item: {0}", stringVal); } IEnumerable<string> catEnum2 = rootElement.Elements().Where(e => e.Name == "Products" && (string)e.Element("CategoryID") == "16").Select(e => (string)e.Element("ModelName")); foreach (string stringVal in catEnum2) { Console.WriteLine("Category 16 item: {0}", stringVal); } } }