Convert Object to XML using LINQ
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的更多相关文章
- C#Object与XML文件或二进制文件之间的转化
Object To Xml 文件 public static bool Serializer<T>(object obj, string path) { FileStream xmlfil ...
- tensorflow基础--LeNet-5测试模型遇到TypeError: Failed to convert object of type <class 'list'> to Tensor
最近在看<TensorFlow 实战Google深度学习框架第二版>这本书,测试LeNet-5这个模型时遇到了TypeError: Failed to convert object of ...
- .Net 4.0 Convert Object to XDocument
将Object转换为XDocment对象 代码如下: C# – Object to XDocument using System; using System.Collections.Generic; ...
- C# ~ 从 XML 到 Linq 到 Linq to XML
.XML 可扩展标记语言 (Extensible Markup Language), 标记 (markup) 是关键部分,是标准通用标记语言 (Standard Generalized Markup ...
- C#操作Xml:linq to xml操作XML
LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...
- C# xml 读xml、写xml、Xpath、Xml to Linq、xml添加节点 xml修改节点
#region XDocument //创建XDocument XDocument xdoc2 = new XDocument(); XElement xel1= new XElement(" ...
- C#使用Linq To XML读取XML,Linq生成XML,Linq创建带属性或带节点XML
using System; using System.Linq; using System.Xml.Linq; namespace Sample2 { class Program { static v ...
- XML To Linq 读取Sharepoint列表中的附件列信息
通过页面查看,列表附件信息列的内容如下: var x = @"<div class='ExternalClass9936DCD1F074427B891D09CFCEFC2AB6'> ...
- [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 ...
随机推荐
- Reorder array to construct the minimum number
Construct minimum number by reordering a given non-negative integer array. Arrange them such that th ...
- 【leetcode】Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- ios nsdataformatter奇怪的问题
用nsdataformatter在中文格式下测试, a 标识的是上午,下午,不是AM,pm. 我在24小时格式的机器上测试,存入了一个 时间,却解析不出来了! static NSString *Hom ...
- nginx 原理&知识
2015年6月4日 17:04:20 星期四 发现两个关于nginx原理的系列文章, 非常好 http://blog.sina.com.cn/s/blog_6d579ff40100wi7p.html ...
- perl q qq qr qw qx 区别与使用方法
1.q 相当于 单引号' ' 转义字符无效 q可以使用()[] {} // ,, 2.qq 相当于" " 转义字符有效 qq可以使用()[] {} // ,, 3.qw 相当于 ...
- ACM/ICPC 之 欧拉回路两道(POJ1300-POJ1386)
两道有关欧拉回路的例题 POJ1300-Door Man //判定是否存在从某点到0点的欧拉回路 //Time:0Ms Memory:116K #include<iostream> #in ...
- ACM/ICPC 之 BFS-广搜+队列入门-抓牛(POJ3278)
这一题是练习广度优先搜索很好的例题,在很多广搜教学中经常用到,放在这里供学习搜索算法的孩纸们看看= = 题目大意:一维数轴上,农夫在N点,牛在K点,假定牛不会移动,农夫要找到这头牛只能够进行以下三种移 ...
- Spring配置数据源
Spring在第三方依赖包中包含了两个数据源的实现类包,其一是Apache的DBCP,其二是 C3P0.可以在Spring配置文件中利用这两者中任何一个配置数据源. DBCP数据源 DBCP类包位于 ...
- 谷歌 Uncaught SecurityError: Failed to execute 'replaceState' on 'History 错误
今天在用sui mobil做一个内联页面的时候遇到了这个问题. 然而这个问题只出现在chrome浏览器中,在火狐中没有一点问题. 他说明的是一个安全问题,chrome中有了新的安全机制
- JS练习题
1.带参数的函数不能重复调用 2.顺序语句: 3.在重复调用中打断 调用用 return 例如: Interval 时间间隔 先清一下 再接着 return:其他的调用也可以加return打 ...