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. swfit中的同步锁

    swfit 中 objective-c 中的@syncronized 这个东西不能用了,应该用 objc_sync_enter(self) 代码 objc_sync_exit(self) 代替!

  2. Windows下安装node

    1.安装node及npm Windows下安装软件都是傻瓜式安装,首先登陆官网(https://nodejs.org/en/)下载对应的node程序,然后双击进行安装.安装过程基本上是点击'Next' ...

  3. python(pyqt)开发环境搭建

    eric+pyqt 安装(python开发工具) 更多 0 Python python Eric是一个开源的.跨平台的python&ruby集成开发环境,基于python和pyqt运行.eri ...

  4. ubuntu vsftp 安装

    1.输入sudo apt-get install vsftpd 回车 这样就安装完毕了,然后去建立一个ftp的帐号,我这里使用的是ftp. 2.输入useradd ftp 回车 输入密码 回车 这样帐 ...

  5. mybatis反向生成sql,基本的增删改查

    用到的几个文件 MyBatisGeneratorProxy.java package com.timestech.wsgk.test.tools; import static org.mybatis. ...

  6. WCF服务跟踪

    如果在开发过程中,WCF服务出现问题,我们可以通过服务引用,然后直接断点调试进去.然而,对于已经发布的服务,出现错误时,寻找错误信息会变得麻烦. 幸好,微软提供了服务跟踪查看器工具 (SvcTrace ...

  7. 【编程题目】有 n 个长为 m+1 的字符串,如果某个字符串的最后 m 个字符与某个字符串的前 m 个字符匹配...

    37.(字符串)有 n 个长为 m+1 的字符串,如果某个字符串的最后 m 个字符与某个字符串的前 m 个字符匹配,则两个字符串可以联接,问这 n 个字符串最多可以连成一个多长的字符串,如果出现循环, ...

  8. IOS- Run Loops

    Run Loops Run loops是线程相关的的基础框架的一部分.一个run loop就是一个事件处理的循环,用来不停的调度工作以及处理输入事件.使用run loop的目的是让你的线程在有工作的时 ...

  9. [Java 基础] 使用java.util.zip包压缩和解压缩文件

    reference :  http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...

  10. @RequestMapping详解

    简介: @RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. RequestM ...