C # LINQ to XML simple example

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.