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. (转载)让XCode运行时自动更新资源

    转自http://goldlion.blog.51cto.com/4127613/1351616 用过XCode的人都知道,XCode有一个臭名昭著的bug——除非你修改了源代码造成了重新编译,否则游 ...

  2. MySQL中的增删改查

    将表cm_application中的state字段类型改为字符串型 alter table  cm_application  modify STATE varchar(50); 将表cm_applic ...

  3. ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)

    八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...

  4. MySQL主从复制数据不一致问题【自增主键】

    前言: 今天遇到主从表不一致的情况,很奇怪为什么会出现不一致的情况,因为复制状态一直都是正常的.最后检查出现不一致的数据都是主键,原来是当时初始化数据的时候导致的.现在分析记录下这个问题,避免以后再遇 ...

  5. ffmpeg-20160813-bin.7z

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  6. ffmpeg-20160508-git-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  7. 记录linux系统用户shell终端操作记录

    在 /etc/profile 最后添加 export HISTTIMEFORMAT='[%F %T]: ' export PROMPT_COMMAND='{ msg=$(history 1 | { r ...

  8. 迷宫问题_BFS_挑战程序设计竞赛p34

    给定一个N*M的迷宫,求从起点到终点的最小步数. N,M<100: 输入: 10 10#S######.#......#..#.#.##.##.#.#........##.##.####.... ...

  9. simpleTree简单使用

    SimpleTree使用起来比较方便,它实现了最基本的树形菜单的功能,包括1个JS文件.1个CSS文件和5个图标文件. 使用时只要将相关文件复制到项目中,并在相应的页面引用它就行,例如: <!D ...

  10. iOS MRC ARC 内存管理

    转自:http://www.jianshu.com/p/48665652e4e4 1. 什么是内存管理 程序在运行的过程中通常通过以下行为,来增加程序的的内存占用 创建一个OC对象 定义一个变量 调用 ...