Convert Object to XML using LINQ. Also the object contains other object list.

Following is the Classes used in our program:

    public class Order
{
public string OrderId { get; set; }
public string OrderNumber { get; set; }
public string OrderDate { get; set; }
public string OrderValue { get; set; }
public string Reference1 { get; set; }
public string Reference2 { get; set; }
public string DeliveryNotes { get; set; }
public string Status { get; set; }
public BillingCustomer OrderBillingCustomer { get; set; }
public EndCustomer OrderEndCustomer { get; set; }
public List<OrderLineItem> OrderLineItem { get; set; }
} public class BillingCustomer
{
public string AccountID { get; set; }
public string AccountName { get; set; }
public string AccountNumber { get; set; }
public string ABN { get; set; }
public string GPID { get; set; }
public string Address { get; set; }
public string Suburb { get; set; }
public string Postcode { get; set; }
public string State { get; set; }
public string Phone { get; set; }
public string Tax { get; set; }
public string Email { get; set; }
public string CreditType { get; set; }
} public class EndCustomer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string Suburb { get; set; }
public string Postcode { get; set; }
public string State { get; set; }
public string Country { get; set; }
} public class OrderLineItem
{
public string LineItemID { get; set; }
public string SKU { get; set; }
public string Title { get; set; }
public string Quantity { get; set; }
public string SalesPriceEx { get; set; }
public string SalesPriceInc { get; set; }
public string DispatchPoint { get; set; }
public string FreightMethod { get; set; }
public string Status { get; set; }
public string OrderID { get; set; }
}

So now you can see the detail of our function:

        public static void GenerateXmlFile(List<Order> orderList, string orderExportXmlPath)
{
XDocument orderDoc = new XDocument(
new XElement("Orders",
from orderItem in orderList
select new XElement("Order",
new XElement("OrderID", orderItem.OrderId),
new XElement("OrderNumber", orderItem.OrderNumber),
new XElement("OrderDate", orderItem.OrderDate),
new XElement("OrderValue", orderItem.OrderValue),
new XElement("Reference1", orderItem.Reference1),
new XElement("Reference2", orderItem.Reference2),
new XElement("DeliveryNotes", orderItem.DeliveryNotes),
new XElement("Account",
new XElement("AccountID", orderItem.OrderBillingCustomer.AccountID),
new XElement("AccountName", orderItem.OrderBillingCustomer.AccountName),
new XElement("AccountNumber", orderItem.OrderBillingCustomer.AccountNumber),
new XElement("ABN", orderItem.OrderBillingCustomer.ABN),
new XElement("GPID", orderItem.OrderBillingCustomer.GPID),
new XElement("Address", orderItem.OrderBillingCustomer.Address),
new XElement("Suburb", orderItem.OrderBillingCustomer.Suburb),
new XElement("Postcode", orderItem.OrderBillingCustomer.Postcode),
new XElement("State", orderItem.OrderBillingCustomer.State),
new XElement("Phone", orderItem.OrderBillingCustomer.Phone),
new XElement("Tax", orderItem.OrderBillingCustomer.Tax),
new XElement("Email", orderItem.OrderBillingCustomer.Email),
new XElement("CreditType", orderItem.OrderBillingCustomer.CreditType)
),
new XElement("EndCustomer",
new XElement("FirstName", orderItem.OrderEndCustomer.FirstName),
new XElement("LastName", orderItem.OrderEndCustomer.LastName),
new XElement("Phone", orderItem.OrderEndCustomer.Phone),
new XElement("Mobile", orderItem.OrderEndCustomer.Mobile),
new XElement("Email", orderItem.OrderEndCustomer.Email),
new XElement("Address1", orderItem.OrderEndCustomer.Address1),
new XElement("Address2", orderItem.OrderEndCustomer.Address2),
new XElement("Address3", orderItem.OrderEndCustomer.Address3),
new XElement("Suburb", orderItem.OrderEndCustomer.Suburb),
new XElement("Postcode", orderItem.OrderEndCustomer.Postcode),
new XElement("State", orderItem.OrderEndCustomer.State),
new XElement("Country", orderItem.OrderEndCustomer.Country)
),
new XElement("LineItems",
from item in orderItem.OrderLineItem
select new XElement("LineItem",
new XElement("LineItemID", item.LineItemID),
new XElement("SKU", item.SKU),
new XElement("Title", item.Title),
new XElement("Quantity", item.Quantity),
new XElement("SalesPriceEx", item.SalesPriceEx),
new XElement("SalesPriceInc", item.SalesPriceInc),
new XElement("DispatchPoint", item.DispatchPoint),
new XElement("FreightMethod", item.FreightMethod)
)
)
)));
orderDoc.Save(orderExportXmlPath);
}

Finally we can get the Xml file:

<?xml version="1.0" encoding="utf-8"?>
<Orders>
<Order>
<OrderID>a00O0000003ZbnEIAS</OrderID>
<OrderNumber>CM000-047</OrderNumber>
<OrderDate>2013-11-07T00:00:00.000Z</OrderDate>
<OrderValue>2519.95</OrderValue>
<Reference1>11</Reference1>
<Reference2>22</Reference2>
<DeliveryNotes>node001</DeliveryNotes>
<Account>
<AccountID>0019000000RXEOVAA5</AccountID>
<AccountName>Test Commercial Customer</AccountName>
<AccountNumber></AccountNumber>
<ABN></ABN>
<GPID></GPID>
<Address></Address>
<Suburb></Suburb>
<Postcode>3000</Postcode>
<State>VIC</State>
<Phone>03 9304 8000</Phone>
<Tax></Tax>
<Email>test@test.com</Email>
<CreditType>Amount</CreditType>
</Account>
<EndCustomer>
<FirstName></FirstName>
<LastName></LastName>
<Phone></Phone>
<Mobile></Mobile>
<Email></Email>
<Address1>11</Address1>
<Address2>22</Address2>
<Address3>33</Address3>
<Suburb></Suburb>
<Postcode></Postcode>
<State>Finalised</State>
<Country></Country>
</EndCustomer>
<LineItems>
<LineItem>
<LineItemID>a01O0000007CQ9BIAW</LineItemID>
<SKU>a12345</SKU>
<Title>product a</Title>
<Quantity>10.0</Quantity>
<SalesPriceEx>10.5</SalesPriceEx>
<SalesPriceInc>11.55</SalesPriceInc>
<DispatchPoint>a02O0000005aDHWIA2</DispatchPoint>
<FreightMethod>Pickup</FreightMethod>
</LineItem>
<LineItem>
<LineItemID>a01O0000007F71EIAS</LineItemID>
<SKU>20000001</SKU>
<Title>FLEETWOOD MAC - THE DANCE</Title>
<Quantity>20.0</Quantity>
<SalesPriceEx>6.43</SalesPriceEx>
<SalesPriceInc>7.07</SalesPriceInc>
<DispatchPoint>a02O0000005aDHWIA2</DispatchPoint>
<FreightMethod>Pickup</FreightMethod>
</LineItem>
<LineItem>
<LineItemID>a01O0000007F71FIAS</LineItemID>
<SKU>928186</SKU>
<Title>HONEY</Title>
<Quantity>50.0</Quantity>
<SalesPriceEx>9.07</SalesPriceEx>
<SalesPriceInc>9.98</SalesPriceInc>
<DispatchPoint>a02O0000005aDHcIAM</DispatchPoint>
<FreightMethod>Pickup</FreightMethod>
</LineItem>
</LineItems>
</Order>
</Orders>

..................

Convert Object to XML using LINQ的更多相关文章

  1. C#Object与XML文件或二进制文件之间的转化

    Object To Xml 文件 public static bool Serializer<T>(object obj, string path) { FileStream xmlfil ...

  2. tensorflow基础--LeNet-5测试模型遇到TypeError: Failed to convert object of type <class 'list'> to Tensor

    最近在看<TensorFlow 实战Google深度学习框架第二版>这本书,测试LeNet-5这个模型时遇到了TypeError: Failed to convert object of ...

  3. .Net 4.0 Convert Object to XDocument

    将Object转换为XDocment对象 代码如下: C# – Object to XDocument using System; using System.Collections.Generic; ...

  4. C# ~ 从 XML 到 Linq 到 Linq to XML

    .XML 可扩展标记语言 (Extensible Markup Language), 标记 (markup) 是关键部分,是标准通用标记语言 (Standard Generalized Markup ...

  5. C#操作Xml:linq to xml操作XML

    LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...

  6. C# xml 读xml、写xml、Xpath、Xml to Linq、xml添加节点 xml修改节点

    #region XDocument //创建XDocument XDocument xdoc2 = new XDocument(); XElement xel1= new XElement(" ...

  7. C#使用Linq To XML读取XML,Linq生成XML,Linq创建带属性或带节点XML

    using System; using System.Linq; using System.Xml.Linq; namespace Sample2 { class Program { static v ...

  8. XML To Linq 读取Sharepoint列表中的附件列信息

    通过页面查看,列表附件信息列的内容如下: var x = @"<div class='ExternalClass9936DCD1F074427B891D09CFCEFC2AB6'> ...

  9. [Ramda] Convert Object Methods into Composable Functions with Ramda

    In this lesson, we'll look at how we can use Ramda's invoker and constructNfunctions to take methods ...

随机推荐

  1. nginx做本地目录映射

    有时候需要访问服务器上的一些静态资源,比如挂载其他设备上的图片到本地的目录,而本地的目录不在nginx根目录下,这个时候就需要简单的做一下目录映射来解决,比如想通过浏览器http://ip/image ...

  2. ABAP 内表的行列转换

    http://www.cnblogs.com/qlp1982/p/3370591.html

  3. linq lanbda表达式的用法

    1. 查询Student表中的所有记录的Sname.Ssex和Class列.select sname,ssex,class from studentLinq:    from s in Student ...

  4. [Android Pro] 临时关闭selinux模式 setenforce 0

    setenforce 0 设置SELinux 成为permissive模式 临时关闭selinux的

  5. MongoDB 基础 -安全性-(权限操作)

    和其他所有数据库一样,权限的管理都差不多一样.mongodb存储所有的用户信息在admin 数据库的集合system.users中,保存用户名.密码和数据库信息.mongodb默认不启用授权认证,只要 ...

  6. iOS开发如何学习前端

    原文链接 前端大概三大块. HTML CSS JavaScript 基本上每个概念在iOS中都有对应的.HTML请想象成只能拉Autolayout或者设置Frame的ViewController.好比 ...

  7. auto(c++11)

    C++primer 第五版,第三章出现了此段程序,求解读附源码:代码1:#include<iostream>#include<string>using namespace st ...

  8. 两个viewport的故事(第一部分)

    原文:http://www.quirksmode.org/mobile/viewports.html 在这个迷你系列的文章里边我将会解释viewport,以及许多重要元素的宽度是如何工作的,比如< ...

  9. NMON中的各项参数指标

    一.NMON中的各项参数指标: SYS_SUMM:显示当前服务器的总体性能情况 Total System I/OStatistics:Avg tps during an interval:显示采集间隔 ...

  10. json数据类型

    JSON 语法规则 JSON 语法是 JavaScript 对象表示法语法的子集. 数据在名称/值对中 数据由逗号分隔 花括号保存对象 方括号保存数组 JSON 名称/值对 JSON 数据的书写格式是 ...