1、XML数据格式:
<?xml version="1.0"?>
<customers>
  <customer>
    <id>ALFKI</id>
    <name>Alfreds Futterkiste</name>
    <address>Obere Str. 57</address>
    <city>Berlin</city>
    <postalcode>12209</postalcode>
    <country>Germany</country>
    <phone>030-0074321</phone>
    <fax>030-0076545</fax>
    <orders>
      <order>
        <id>10643</id>
        <orderdate>1997-08-25T00:00:00</orderdate>
        <total>814.50</total>
      </order>
      <order>
        <id>10692</id>
        <orderdate>1997-10-03T00:00:00</orderdate>
        <total>878.00</total>
      </order>
      <order>
        <id>10702</id>
        <orderdate>1997-10-13T00:00:00</orderdate>
        <total>330.00</total>
      </order>
      <order>
        <id>10835</id>
        <orderdate>1998-01-15T00:00:00</orderdate>
        <total>845.80</total>
      </order>
      <order>
        <id>10952</id>
        <orderdate>1998-03-16T00:00:00</orderdate>
        <total>471.20</total>
      </order>
      <order>
        <id>11011</id>
        <orderdate>1998-04-09T00:00:00</orderdate>
        <total>933.50</total>
      </order>
    </orders>
  </customer>
</customers>
2、Customer类:
public class Customer
    {
        public string CustomerID { get; set; }
        public string CompanyName { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Region { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }
        public string Phone { get; set; }
        public string Fax { get; set; }
        public Order[] Orders { get; set; }
    }
3、读取数据
List<Customer> lists = (from customer in XDocument.Load("Customers.xml").Root.Elements("customer")
                                    select new Customer
                                    {
                                        CustomerID = (string)customer.Element("id"),
                                        CompanyName = (string)customer.Element("name"),
                                        Address = (string)customer.Element("address"),
                                        City = (string)customer.Element("city"),
                                        Region = (string)customer.Element("region"),
                                        PostalCode = (string)customer.Element("postalcode"),
                                        Country = (string)customer.Element("country"),
                                        Phone = (string)customer.Element("phone"),
                                        Fax = (string)customer.Element("fax"),
                                        Orders = (from order in customer.Elements("orders").Elements("order")
                                                  select new Order
                                                  {
                                                      OrderID = (int)order.Element("id"),
                                                      OrderDate = (DateTime)order.Element("orderdate"),
                                                      Total = (decimal)order.Element("total")
                                                  }).ToArray()
                                    }).ToList();

Linq读取XML数据的更多相关文章

  1. wcf序列化大对象时报错:读取 XML 数据时,超出最大

    错误为: 访问服务异常:格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出 错: request.InnerException 消息是“反序 ...

  2. InnerException 消息是“反序列化对象 属于类型 *** 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。(注意细节)

    WEB站点在调用我们WCF服务的时候,只要传入的参数过长,就报如下错误: 格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: formD ...

  3. Web Service 或 WCF调用时读取 XML 数据时,超出最大字符串内容长度配额(8192)解决方法

    1.调用服务时服务 当我们使用 Web Service 或 WCF 服务时,常把读取的数据转化为string类型(xml格式),当数据量达到一 定数量时,会出现以下异常: 错误:格式化程序尝试对消息反 ...

  4. 读取 XML 数据时,超出最大字符串内容长度配额 (8192)

    格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://www.thermo.com/informatics/xmlns/limswebservice 进行反序列化时出错: Process ...

  5. 格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: GetLzdtArticleResult。InnerException 消息是“反序列化对象 属于类型 lzdt.DTO.Dtolzdt[] 时出现错误。读取 XML 数据时,超出最大

    当遇到这个错误的时候郁闷了好长时间报错是字符串长度过大可是修改了MaxStringContentLength”属性的值却不起作用最后才发现还是因为配置文件配置的问题在服务端 格式化程序尝试对消息反序列 ...

  6. XML序列化器读取XML数据

    PS:标题我还真的不知道该怎么取比较好,大家将就下吧^_^ 场景:上周接到一个任务,要求我把ASP写的会员充值功能,用ASP.NET复制一遍,没有给我需求文档,就是让我根据代码去分析业务逻辑,然后看到 ...

  7. 用php读取xml数据

    parser是php内置的一个用来处理xml的解析器,它的工作由三个事件组成:起始标签. 读取数据.结束标签. 也就是说在对xml进行处理的时候每当遇到起始标签.数据和结束标签的时候函数会做相应的动作 ...

  8. [drp 4] 使用dom4j,读取XML数据,保存至数据库

    导读:上篇文章介绍了用XML文件配置数据库的连接,然后通过读取XML文件连接数据库的内容,本篇博客介绍读取XML文件,进行数据持久化的操作.PS:从某种意义上来说,经过Scheme校正的XML文件,本 ...

  9. 使用Java读取XML数据

    ---------------siwuxie095 工程名:TestReadXML 包名:com.siwuxie095.xml 类名:ReadXML.java 打开资源管理器,在工程 TestRead ...

随机推荐

  1. Codefroces Educational Round 27 845G Shortest Path Problem?

    Shortest Path Problem? You are given an undirected graph with weighted edges. The length of some pat ...

  2. 一个Web报表项目的性能分析和优化实践(七):性能监测工具JavaMelody

    简介 JavaMelody 能够监测Java或Java EE应用程序服务器,并以图表的方式显示:Java内存和Java CPU使用情况,用户Session数量,JDBC连接数,和http请求.sql请 ...

  3. absolute、relative,toggle()

    測试代码例如以下: <div> <div class="global">不应用样式</div> <div class="glob ...

  4. 源代码编译安装CloudStack 4.2

    基于CentOS 6.4安装CloudStack 环境配置 # yum -y update # yum -y upgrade 安装NTP,jdk 1.7, tomcat 6, mysql,git等服务 ...

  5. resource-color 的引用

    1.在xml文件中引用 android:textColor="@color/tv_top_title_color" 2.在代码中引用 1)在color.xml中定义 <?xm ...

  6. [LuoguU41039]PION后缀自动机 树链剖分+动态开点线段树

    链接 刚开始看出题人题解都吓蒙掉了,还以为是什么难题,结果就一板子题 思路:对每一个文件名开一棵线段树,然后树剖即可 #include<bits/stdc++.h> #define REP ...

  7. Vue 消息无缝滚动

    vue实现消息向上无缝滚动效果 <ul class="new-list" :class="{anim:animate}" @mouseenter=&quo ...

  8. 推广一下新Blog www.hrwhisper.me

    新博客地址:www.hrwhisper.me 欢迎互访加友链~

  9. 【例题 8-1 UVA 120 】Stacks of Flapjacks

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从大到小安排. 显然想让第i大的数字归位 只要让他翻到最上面,然后再翻回来就ok了 即operate(pos[i]) -> o ...

  10. [Node & Testing] Intergration Testing with Node Express

    We have express app: import _ from 'lodash' import faker from 'faker' import express from 'express' ...