Convert XML to Object using LINQ
Class and Xml : Please see my another article. http://www.cnblogs.com/mingmingruyuedlut/p/3436803.html
Following is the mainly function:
public static List<Order> GetOrderListFromXml(string orderUpsertXmlPath)
{
List<Order> orderList = new List<Order>();
if (File.Exists(orderUpsertXmlPath))
{
XDocument doc = XDocument.Load(orderUpsertXmlPath); orderList = (from order in doc.Root.Elements("Order")
select new Order
{
OrderId = order.Element("OrderID").Value,
OrderNumber = order.Element("OrderNumber").Value,
OrderDate = order.Element("OrderDate").Value,
OrderValue = order.Element("OrderValue").Value,
Reference1 = order.Element("Reference1").Value,
Reference2 = order.Element("Reference2").Value,
DeliveryNotes = order.Element("DeliveryNotes").Value,
Status = order.Element("Status").Value,
OrderEndCustomer = GetEndCustomerInfoFromXml(order),
OrderLineItem = GetLineItemListFromXml(order)
}).ToList();
} return orderList;
} static EndCustomer GetEndCustomerInfoFromXml(XElement order)
{
EndCustomer endCustomerInfo = new EndCustomer();
endCustomerInfo.FirstName = order.Element("EndCustomer").Element("FirstName").Value;
endCustomerInfo.LastName = order.Element("EndCustomer").Element("LastName").Value;
endCustomerInfo.Phone = order.Element("EndCustomer").Element("Phone").Value;
endCustomerInfo.Mobile = order.Element("EndCustomer").Element("Mobile").Value;
endCustomerInfo.Email = order.Element("EndCustomer").Element("Email").Value;
endCustomerInfo.Address1 = order.Element("EndCustomer").Element("Address1").Value;
endCustomerInfo.Address2 = order.Element("EndCustomer").Element("Address2").Value;
endCustomerInfo.Address3 = order.Element("EndCustomer").Element("Address3").Value;
endCustomerInfo.Suburb = order.Element("EndCustomer").Element("Suburb").Value;
endCustomerInfo.Postcode = order.Element("EndCustomer").Element("Postcode").Value;
endCustomerInfo.State = order.Element("EndCustomer").Element("State").Value;
endCustomerInfo.Country = order.Element("EndCustomer").Element("Country").Value;
return endCustomerInfo;
} static List<OrderLineItem> GetLineItemListFromXml(XElement order)
{
List<OrderLineItem> lineItemList = new List<OrderLineItem>(); lineItemList = (from item in order.Elements("LineItems").Elements("LineItem")
select new OrderLineItem
{
OrderID = order.Element("OrderID").Value,
LineItemID = item.Element("LineItemID").Value,
SKU = item.Element("SKU").Value,
Title = item.Element("Title").Value,
Quantity = item.Element("Quantity").Value,
SalesPriceEx = item.Element("SalesPriceEx").Value,
SalesPriceInc = item.Element("SalesPriceInc").Value,
DispatchPoint = item.Element("DispatchPoint").Value,
FreightMethod = item.Element("FreightMethod").Value,
Status = item.Element("Status").Value
}).ToList(); return lineItemList;
}
Another article link about this function is :
http://www.codeproject.com/Tips/366993/Convert-XML-to-Object-using-LINQ
.....
Convert XML to Object using LINQ的更多相关文章
- RCE via XStream object deserialization && SECURITY-247 / CVE-2016-0792 XML reconstruction Object Code Inject
catalogue . Java xStream . DynamicProxyConverter . java.beans.EventHandler . RCE via XStream object ...
- 无法将类型“System.Nullable`1”强制转换为类型“System.Object”。LINQ to Entities 仅支持强制转换 EDM 基元或枚举类型。
在一个项目中使用LINQ和EF时出现了题目所示的异常,搜索了很多资料都找不到解决办法,主要是因为EF方面的知识欠缺. 先将情况记录如下,以供以后参考. 查询主要设计两张表,由外键关联: 在进行下面的查 ...
- JAXB 操作XML 与 Object
Java Architecture for XML Binding) 是一个业界的标准,是一项能够依据XML Schema产生Java类的技术.是一种xml与object映射绑定技术标准. JDK5下 ...
- ISO 8601: Delphi way to convert XML date and time to TDateTime and back (via: Stack Overflow)
Recently I needed a way of concerting back and forth ISO 8601 DateTime values used in XML from Delph ...
- Entity Framework Code First 在Object Join Linq查询时出现全表查询的语句。
最近一个项目,使用微软的Entity Framework的ORM框架的项目,部署到现场后,出现了系统缓慢,多个客户端的内存溢出崩溃的问题. 打开了SQL Server Profiler(SQL Ser ...
- how to convert Map to Object in js
how to convert Map to Object in js Map to Object just using the ES6 ways Object.fromEntries const lo ...
- LinqToXml (一) Create Xml file By Dom /Linq
目前,在xml 应用编程领域比较流行的开发模型是W3C 提供的DOM(文档对象模型),在.net Framework 通过命名空间 System.Xml 对该技术提供了支持.随着Linq to XMl ...
- XML基础学习02<linq to xml>
Linq to XML的理解 1:这是一种比较好的操作Xml的工具. àXDocument 文档 àXElement 元素 àXAttribute 属性 àXText 文本 2:这里还是和我们之前创建 ...
- LINQ以及LINQ to Object 和LINQ to Entities
LINQ的全称是Language Integrated Query,中文译成“语言集成查询”,是一种查询技术. LINQ查询通过提供一种跨各种数据源和数据格式使用数据的一致模型,简化了查询过程.LIN ...
随机推荐
- Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- H-Index I & II
H-Index I Given an array of citations (each citation is a non-negative integer) of a researcher, wri ...
- 在Py文件中引入django环境
复制manage.py中的相关代码即可并将文件置于Project文件夹(与manage.py同位置)下 示例: #! /usr/bin/env python # -*- coding:utf- -*- ...
- Unity3d《Shader篇》描边
Shader "Custom/OutLine" {Properties { _Color ("Main Color", Color) = (.5,.5,.5,1 ...
- Unity3D 给模型偏移纹理
给模型偏移纹理 using UnityEngine; using System.Collections; [RequireComponent(typeof(Renderer))] public cla ...
- 解读Unity中的CG编写Shader系列八(多光源漫反射)
转自http://www.itnose.net/detail/6117338.html 前文中完成最简单的漫反射shader只是单个光源下的漫反射,而往往场景中不仅仅只有一个光源,那么多个光源的情况下 ...
- MFC 对话框添加菜单
1.在Resource View 里右击菜单里选择Add Resource,选择menu,添加一个IDR_MENU1的菜单.在编辑器编辑菜单,添加菜单项,命名各个菜单项的ID. 2.在所要添加菜单的对 ...
- 1.JS设计模式-this,call&apply
1. this,call&apply 1.1 this this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用. 1.1.1 普通函数调 ...
- metro压缩和解压文件
在1.zip中增加一张新图片StorageFile jpg = await KnownFolders.PicturesLibrary.GetFileAsync("1.jpg"); ...
- Python缩小图像
LyncLynn用途: 缩小图像 # -*- coding: UTF-8 -*- #Version: V1.0 #Author:lynclynn #CreateDate:20151201 #Updat ...