検索

LinqToXML~读XML文件

Jul 06, 2016 pm 01:30 PM
linqxml現れる私たち書類

linq的出现,带给我们的是简结,快速,可读性,它由linq to sql,linq to object,linq to xml组成,我的博客之前有对linq to sql的讲解,而今天,我将讲一个linq to xml架构,将对于操作XML文件进行读写操作,它会让你像使用linq to sql一样,操作你的xml

linq的出现,带给我们的是简结,快速,可读性,它由linq to sql,linq to object,linq to xml组成,我的博客之前有对linq to sql的讲解,而今天,我将讲一个linq to xml架构,将对于操作XML文件进行读写操作,它会让你像使用linq to sql一样,操作你的xml文件,OK,我们来看一下,下面的一个XML文件。

复制代码
<?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\资料\thrift文件\thrift文件\testjava.thrift</filepath>
    <currentseek>1297</currentseek>
    <clientkey>8C:89:A5:E1:89:3D</clientkey>
    <ordernumber>1</ordernumber>
    <occurtime>2014/1/26 15:32:46</occurtime>
    <size>0</size>
  </datasync>
  <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\资料\thrift文件\thrift文件\thrift-0.8.0.exe</filepath>
    <currentseek>4013056</currentseek>
    <clientkey>8C:89:A5:E1:89:3D</clientkey>
    <ordernumber>1</ordernumber>
    <occurtime>2014/1/26 15:32:46</occurtime>
    <size>0</size>
  </datasync>
  <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\资料\thrift文件\thrift文件\thrift_白皮书.pdf</filepath>
    <currentseek>441217</currentseek>
    <clientkey>8C:89:A5:E1:89:3D</clientkey>
    <ordernumber>1</ordernumber>
    <occurtime>2014/1/26 15:32:46</occurtime>
    <size>0</size>
  </datasync>
  <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\资料\thrift文件\thrift文件\thrift命令.txt</filepath>
    <currentseek>131</currentseek>
    <clientkey>8C:89:A5:E1:89:3D</clientkey>
    <ordernumber>1</ordernumber>
    <occurtime>2014/1/26 15:32:46</occurtime>
    <size>0</size>
  </datasync>
</arrayofdatasync>
复制代码

这个文件是thrift自动为我们生成的,现在我们使用linq to xml来读这个文件里的内容读出来,当然你也可以使用XML反序列化的方式,将它反序列化成对象,再读出。

复制代码
  System.Console.WriteLine("Loading XML data...");
            var data =
                (from e in XElement.Load("D:\\path\\资料\\thrift文件\\thrift文件\\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()));
复制代码

而输出的结果如下:

上面的例子中,我们的XML文件使用的是元素的方式,也可以使用属性的形式,代码只要稍作修改即可。


声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン