LinqToXML ~ Read XML files and linqtoxml read xml

Source: Internet
Author: User

LinqToXML ~ Read XML files and linqtoxml read xml

The emergence of linq brings us a simple knot, fast, and readable. It consists of linq to SQL, linq to object, and linq to XML, my blog has previously explained how to operate the SQL statements. Today, I will talk about a linq to xml architecture that reads and writes the XML files, it will let you operate your xml file like using linq to SQL. Let's take a look at the following XML file.

<? Xml version = "1.0"?> <ArrayOfDataSync xmlns: xsi =" http://www.w3.org/2001/XMLSchema -Instance "xmlns: xsd =" http://www.w3.org/2001/XMLSchema "> <DataSync> <__ isset> <IsSync> true </IsSync> <FilePath> true </FilePath> <CurrentSeek> true </CurrentSeek> <ClientKey> true </ClientKey> <OrderNumber> true </OrderNumber> <OccurTime> true </OccurTime> <Name> false </Name> <Size> false </Size> <LastDate> false </LastDate> </_ isset> <IsSync> true </IsSync> <FilePath> d: \ path \ document \ thrift file \ testJava. thrift </FilePath> <CurrentSeek> 1297 </CurrentSeek> <ClientKey> 8C: 89: A5: E1: 89: 3D </ClientKey> <OrderNumber> 1 </OrderNumber> <OccurTime> 15:32:46 </OccurTime> <Size> 0 </Size> </DataSync> <__isset> <IsSync> true </IsSync> <FilePath> true </FilePath> <CurrentSeek> true </CurrentSeek> <ClientKey> true </ClientKey> <OrderNumber> true </OrderNumber> <OccurTime> true </OccurTime> <Name> false </Name> <Size> false </Size> <LastDate> false </LastDate> </_ isset> <isSync> true </IsSync> <FilePath> d: \ path \ material \ thrift file \ thrift-0.8.0.exe </FilePath> <CurrentSeek> 4013056 </CurrentSeek> <ClientKey> 8C: 89: A5: E1: 89: 3D </ClientKey> <OrderNumber> 1 </OrderNumber> <OccurTime> 15:32:46 </OccurTime> <Size> 0 </Size> </DataSync> <__isset> <IsSync> true </IsSync> <FilePath> true </FilePath> <CurrentSeek> true </CurrentSeek> <ClientKey> true </ClientKey> <OrderNumber> true </OrderNumber> <OccurTime> true </OccurTime> <Name> false </Name> <Size> false </Size> <LastDate> false </LastDate> </_ isset> <isSync> true </IsSync> <FilePath> d: \ path \ Documents \ thrift file \ thrift_white skin book </FilePath> <CurrentSeek> 441217 </CurrentSeek> <ClientKey> 8C: 89: A5: E1: 89: 3D </ClientKey> <OrderNumber> 1 </OrderNumber> <OccurTime> 15:32:46 </OccurTime> <Size> 0 </Size> </DataSync> <__isset> <IsSync> true </IsSync> <FilePath> true </FilePath> <CurrentSeek> true </CurrentSeek> <ClientKey> true </ClientKey> <OrderNumber> true </OrderNumber> <OccurTime> true </OccurTime> <Name> false </Name> <Size> false </Size> <LastDate> false </LastDate> </_ isset> <isSync> true </IsSync> <FilePath> d: \ path \ Documents \ thrift file \ thriftcommand .txt </FilePath> <CurrentSeek> 131 </CurrentSeek> <ClientKey> 8C: 89: A5: E1: 89: 3D </ClientKey> <OrderNumber> 1 </OrderNumber> <OccurTime> 15:32:46 </OccurTime> <Size> 0 </Size> </DataSync> </ArrayOfDataSync>

This file is automatically generated by thrift. Now we use linq to xml to read the content in this file. Of course, you can also use XML deserialization, deserialize it into an object and read it again.

System. console. writeLine ("Loading XML data... "); var data = (from e in XElement. load ("D :\\ path \ materials \ thrift file \ fileUploadLog. xml_log.xml "). elements ("DataSync") select new DataSync {OrderNumber = Convert. toInt32 (e. element ("OrderNumber "). value), IsSync = Convert. toBoolean (e. element ("IsSync "). value), FilePath = e. element ("FilePath "). value, CurrentSeek = Convert. toInt64 (e. element ("CurrentSeek "). value), ClientKey = e. element ("ClientKey "). value, OccurTime = e. element ("OccurTime "). value ,}). toList (); data. forEach (I => System. console. writeLine (I. toString ()));

The output result is as follows:

In the above example, our XML file uses the element method or the attribute form, and the code can be slightly modified.

Related Article

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.