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. solr单机环境配置并包含外部单机zookeeper

    首先和之前一样下载solr-5.3.1.tgz,然后执行下面命令释放文件并放置在/usr/目录下: $ .tgz $ /usr/ $ cd /usr/solr- 这个时候先不用启动solr,因为单机模 ...

  2. 13. javacript高级程序设计-事件

    1. 事件 1.1 事件流 事件流描述的是从页面中接受事件的顺序,IE的事件是冒泡流,而Netscape Communicator的事件流是事件捕捉流. 1.1.1 事件冒泡 <!DOCTYPE ...

  3. [转] Android利用tcpdump抓包

    原文链接:http://mysuperbaby.iteye.com/blog/902201 Android利用tcpdump抓包 博客分类: Android AndroidAccessGoHTML  ...

  4. 最简单粗暴的http文件列表

    :]: port = ])else: port = 8000server_address = ('127.0.0.1', port)Handler.protocol_version = Protoco ...

  5. centos6.5Xen4.2安装

    官方安装文档:http://xen.crc.id.au/support/guides/install/ 一.环境说明 1. 本文采用CentOS6.5 x64,安装开发包及开发工具. 2. 关闭sel ...

  6. Effective C++ -----条款42:了解typename的双重意义

    声明template参数时,前缀关键字class和typename可互换. 请使用关键字typename标识嵌套从属类型名称:但不得在base class lists(基类列)或member init ...

  7. 1.SQL语句入门

    --SQL语句入门-- --1.sql语言是解释语言 --2.它不区分大小写 --3.没有"",所有字符或者字符串都使用''包含 --4.sql里面也有类似于c#的运算符 -- 算 ...

  8. 【python】入门学习(六)

    type() #检查变量或值得数据类型 >>> type(5) <class 'int'> 序列:包括字符串.元组和列表.序列都可以用索引.切片.len()(计算元素个数 ...

  9. 【linux】linux脚本中#!/bin/sh的含义

    来源:百度知道 #! /bin/sh 是指此脚本使用,/bin/sh来解释执行,#!是特殊的表示符,其后面根的是此解释此脚本的shell的路径.

  10. HDU 5881 Tea -2016 ICPC 青岛赛区网络赛

    题目链接 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不大于1. 你无法测量壶里剩下水的 ...