LINQ to XML Memo 1 (use of xdocument and xelement)

Source: Internet
Author: User

0. Preparation: Define products. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Products>
<Product ID = "product1" Category = "book">
<Name> programing something </Name>
<Price> 2000 </price>
<Publishdate> 2008/10/10 </publishdate>
<Authors>
<Author> Tarou book </author>
<Author> Hanako book </author>
</Authors>
</Product>
<Product ID = "product2" Category = "book">
<Name> administrating something </Name>
<Price> 5000 </price>
<Publishdate> 2008/10/12 </publishdate>
<Authors>
<Author> kotarou book1 </author>
<Author> kohanako book2 </author>
</Authors>
</Product>
<Product ID = "product3" Category = "novel">
<Name> suspection novel </Name>
<Price> 500 </price>
<Publishdate> 2007/12/12 </publishdate>
<Authors>
<Author> jirou Tarou </author>
</Authors>
</Product>
<Product ID = "product4" Category = "novel">
<Name> fantasy novel </Name>
<Price> 540 </price>
<Publishdate> 2008/9/14 </publishdate>
<Authors>
<Author> Tarou book </author>
</Authors>
</Product>
<Product ID = "product5" Category = "Cram">
<Name> study English </Name>
<Price> 2400 </price>
<Publishdate> 2008/9/14 </publishdate>
<Authors>
<Author> English Tarou </author>
<Author> English jirou </author>
</Authors>
</Product>
</Products>

 

1. LINQ to XML
1.1 load XML files

Use the load method of xdocument and xelement to read XML files and perform initial transformation of xdocument and xelement.

Xelement ELEM = xelement. load (@ "D:/visualstudioproject2008/test/linq2xml01/products. XML "); <br/> console. writeline (ELEM); </P> <p> xdocument Doc = xdocument. load (@ "D:/visualstudioproject2008/test/linq2xml01/products. XML "); <br/> console. writeline (Doc. tostring ());

 

1.2 select processing

As shown in the following code, you can use LINQ to query XML.

Xelement products = xelement. load (@ "D:/visualstudioproject2008/test/linq2xml01/products. XML "); </P> <p> var query = from P in products. elements ("product") <br/> where (string) p. attribute ("category") = "book" <br/> select new <br/>{< br/> id = (string) p. attribute ("ID"), <br/> name = (string) p. element ("name"), <br/> price = (decimal) p. element ("price") <br/>}; </P> <p> foreach (VAR item in query) <br/>{< br/> console. writeline (item); <br/>}

 

The execution result is as follows:

{Id = product1, name = programing something, price = 2000}
{Id = product2, name = administrating something, price = 5000}

 

More complex applications, such as join a String Array (a set of books.

 VaR categories = new string [] {"book", "novel" };< br/> // write P directly in on. element ("category") cannot be compiled. <br/> // It must be displayed and compared after being converted to the string type: (string) p. element ("category") <br/> var query = from P in products. elements ("product") <br/> let xcategory = (string) p. attribute ("category ") <br/> join C in categories <br/> On xcategory equals C <br/> orderby xcategory <br/> select new <br/>{< br/> name = (string) p. element ("name"), <br/> price = (decimal) p. element ("price"), <br/> Category = xcategory <br/>}; <br/> foreach (VAR item in query) <br/> {<br/> console. writeline (item); <br/>}

 

Execution result:

{Name = programing something, price = 2000, Category = book}
{Name = administrating something, price = 5000, Category = book}
{Name = suspection novel, price = 500, Category = novel}
{Name = fantasy novel, price = 540, Category = novel}

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.