Data.xml
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Data>3 < Products>4 <ProductName= "West Side story" Price= "9.99"SupplierID= "1" />5 <ProductName= "Assassins" Price= "14.99"SupplierID= "2" /> 6 <ProductName= "Frogs" Price= "13.99"SupplierID= "1" />7 <ProductName= "Sweeney Todd" Price= "10.99"SupplierID= "3" />8 </ Products>9 Ten <Suppliers> One <SupplierName= "solely Sondheim"SupplierID= "1" /> A <SupplierName= "Cd-by-cd-by-sondheim"SupplierID= "2" /> - <SupplierName= "Barbershop CDs"SupplierID= "3" /> - </Suppliers> the </Data>
Find products with a price greater than 10 through LINQ to XML, and print the vendor name and product name;
1XDocument doc = Xdocument.load ("Data.xml");2 varFiltered = fromPinchDoc. Descendants ("Product")3Join SinchDoc. Descendants ("Supplier")4On (int) P.attribute ("SupplierID")5Equals (int) S.attribute ("SupplierID")6 where(decimal) P.attribute (" Price") >Ten7 Select New8 {9ProductName = (string) P.attribute ("Name"),TenSupplierName = (string) S.attribute ("Name") One }; A - foreach(varVinchfiltered) - { theConsole.WriteLine ("suppliername={0}, Productname={1}", V.suppliername, v.productname); -}
Output
Suppliername=cd-by-cd-by-sondheim, Productname=assassins
Suppliername=solely Sondheim, Productname=frogs
Suppliername=barbershop CDs, Productname=sweeney Todd
Resources
1, in-depth understanding of C # (2nd edition);
C # LINQ to XML simple example