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.