Linq读取XML数据
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数据的更多相关文章
- wcf序列化大对象时报错:读取 XML 数据时,超出最大
错误为: 访问服务异常:格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出 错: request.InnerException 消息是“反序 ...
- InnerException 消息是“反序列化对象 属于类型 *** 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。(注意细节)
WEB站点在调用我们WCF服务的时候,只要传入的参数过长,就报如下错误: 格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: formD ...
- Web Service 或 WCF调用时读取 XML 数据时,超出最大字符串内容长度配额(8192)解决方法
1.调用服务时服务 当我们使用 Web Service 或 WCF 服务时,常把读取的数据转化为string类型(xml格式),当数据量达到一 定数量时,会出现以下异常: 错误:格式化程序尝试对消息反 ...
- 读取 XML 数据时,超出最大字符串内容长度配额 (8192)
格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://www.thermo.com/informatics/xmlns/limswebservice 进行反序列化时出错: Process ...
- 格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: GetLzdtArticleResult。InnerException 消息是“反序列化对象 属于类型 lzdt.DTO.Dtolzdt[] 时出现错误。读取 XML 数据时,超出最大
当遇到这个错误的时候郁闷了好长时间报错是字符串长度过大可是修改了MaxStringContentLength”属性的值却不起作用最后才发现还是因为配置文件配置的问题在服务端 格式化程序尝试对消息反序列 ...
- XML序列化器读取XML数据
PS:标题我还真的不知道该怎么取比较好,大家将就下吧^_^ 场景:上周接到一个任务,要求我把ASP写的会员充值功能,用ASP.NET复制一遍,没有给我需求文档,就是让我根据代码去分析业务逻辑,然后看到 ...
- 用php读取xml数据
parser是php内置的一个用来处理xml的解析器,它的工作由三个事件组成:起始标签. 读取数据.结束标签. 也就是说在对xml进行处理的时候每当遇到起始标签.数据和结束标签的时候函数会做相应的动作 ...
- [drp 4] 使用dom4j,读取XML数据,保存至数据库
导读:上篇文章介绍了用XML文件配置数据库的连接,然后通过读取XML文件连接数据库的内容,本篇博客介绍读取XML文件,进行数据持久化的操作.PS:从某种意义上来说,经过Scheme校正的XML文件,本 ...
- 使用Java读取XML数据
---------------siwuxie095 工程名:TestReadXML 包名:com.siwuxie095.xml 类名:ReadXML.java 打开资源管理器,在工程 TestRead ...
随机推荐
- javafx image zoom
public class EffectTest extends Application { private final ImageView imageView = new ImageView(); p ...
- node搭建服务器
创建简单的服务器,可以简单连接 var http = require("http"); var server = http.createServer(); server.on(&q ...
- 用css让元素隐藏的几种办法
display:none; //能隐藏并不占空间 visibility:hidden; //隐藏但占据空间 opacity:0; position:absolute 移动到不在页面显示的地方
- Spring模块作用
0.模块整理 Spring模块整理(http://www.kuqin.com/shuoit/20150805/347434.html) 模块名 作用 资料 aop spring的面向切面编程,提供A ...
- Java第三方工具库/包汇总
一.科学计算或矩阵运算库 科学计算包: JMathLib是一个用于计算复杂数学表达式并能够图形化显示计算结果的Java开源类库.它是Matlab.Octave.FreeMat.Scilab的一个克隆, ...
- Winform 获取相对路径 C#
///获取相对路径 ///例如:System.Windows.Forms.Application.StartupPath = "E:\App\CheckingMachine\QueryMac ...
- vue移动端上拉加载更多
LoadMore.vue <template> <div class="load-more-wrapper" @touchstart="touchSta ...
- 代码生成器实现的Entity,Dao,Service,Controller,JSP神器(含代码附件)
package com.flong.codegenerator; import java.sql.Connection; import java.sql.DatabaseMetaData; impor ...
- l洛谷 P3926 SAC E#1 - 一道不可做题 Jelly
P3926 SAC E#1 - 一道不可做题 Jelly 题目背景 SOL君(炉石主播)和SOL菌(完美信息教室讲师)是好朋友. 题目描述 SOL君很喜欢吃蒟蒻果冻.而SOL菌也很喜欢蒟蒻果冻. 有一 ...
- 用实力让情怀落地!阅兵前线指挥车同款电视TCL H8800受捧
近期.一则重磅消息刷爆了平面媒体.微博.朋友圈等各个传播渠道:TCL曲面电视H8800正式入驻大阅兵前线指挥车以及国旗护卫队荣誉室.宣告代表眼下中国彩电业最高技术水准的曲面电视,正式走上大阅兵 ...