Convert XML to Object using LINQ
2013-11-22 18:20
477 查看
Class and Xml : Please see my another article. /article/4898653.html
Following is the mainly function:
Another article link about this function is :
http://www.codeproject.com/Tips/366993/Convert-XML-to-Object-using-LINQ
.....
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 Object to XML using LINQ
- Convert XML data to object and back using serialization
- LINQ 首部曲 : LINQ To Object Part 1 - Using VB.NET 2008
- How To Submit Form Data by Using XMLHTTP or ServerXMLHTTP Object
- How to convert a simple DLINQ Query to a data set object
- JsonHelper developed by using Newtonsoft.Json.NET, Deserialize to <T> object , XmlToJson/JsonToXml, QuoteName by using JToken Path.
- An Extensive Examination of LINQ: Querying and Searching XML Documents Using LINQ to XML
- LINQ首部曲: LINQ To Object Part 2 - Using VB.NET
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML
- How to convert Word table into Excel using OpenXML
- [Ramda] Convert a QueryString to an Object using Function Composition in Ramda
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML
- Visual C# 2008+SQL Server 2005 数据库与网络开发-- 11.5 LINQ To XML
- linq to xml(添加节点3)
- linq to xml基础
- linq to xml
- object to xml
- The Classical Example using DOM to operate a XML File
- LINQ系列:Linq to Object限制操作符
- How to Convert 10g Single-Instance database to 10g RAC using Manual Conversion procedure (文档 ID 7474