찾다
php教程php手册LinqToXML~读XML文件

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 Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

SublimeText3 Linux 새 버전

SublimeText3 Linux 새 버전

SublimeText3 Linux 최신 버전