linq to xml

 

XDocument doc = XDocument.Load(@"C:\Users\Administrator\Documents\Visual Studio 2013\Projects\MYWEBSITE\IComparer\data.xml");
            var filtered = from p in doc.Descendants("Product")
                           join s in doc.Descendants("Supplier")
                           on (int)p.Attribute("SupplierID")
                           equals (int)s.Attribute("SupplierID")
                           where (decimal)p.Attribute("Price") > 10
                           orderby (string)s.Attribute("Name"),
                           (string)p.Attribute("Name")
                           select new { 
                           SupplierName=(string)s.Attribute("Name"),
                           ProductName=(string)p.Attribute("Name")
                           };
            foreach (var v in filtered)
            {
                Console.WriteLine("Supplier={0};Product={1}",v.SupplierName,v.ProductName);
            }
            Console.Read();

 

posted @ 2015-09-25 14:39  许大虾  阅读(190)  评论(0编辑  收藏  举报