标签:
data.xml
<?xml version="1.0" encoding="utf-8" ?> <Data> <Products> <Product Name="West Side Story" Price="9.99m" SuppelierID="1"/> <Product Name="Assassins" Price="14.99m" SuppelierID="2"/> <Product Name="Frogs" Price="13.99m" SuppelierID="1"/> <Product Name="Sweeney Todd" Price="9.99m" SuppelierID="3"/> </Products> <Suppliers> <Supplier Name="Solely Sondheim" SupplierID="1"/> <Supplier Name="CD-by-CD-by-Sondheim" SupplierID="2"/> <Supplier Name="Barbershop CDs" SupplierID="3"/> </Suppliers> </Data>
应用
using System.Xml.Linq; namespace ConsoleApplication1 { class XML_LINQ { static void Main() { //将产品和供货商连接起来,价格高于10美元,先按供货商名排序,在按产品名排序,最后打印供货商名和产品名 //供货商和产品编码使用XML文件 XDocument doc = XDocument.Load("data.xml"); //XDocument使用命名空间System.Xml.Linq; 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)s.Attribute("Name") }; foreach (var v in filtered) { Console.WriteLine("Supplier={0};Product={1}", v.SupplierName, v.ProductName); } } } }
标签:
原文地址:http://www.cnblogs.com/Sukie-s-home/p/5694584.html