• 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>

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

  • 相关阅读:
    filter函数示例
    组件里v-for示例
    操作数组的函数简介
    class绑定对象改进版
    python 全栈开发,Day6(函数进阶)
    python 全栈开发,Day5(函数初识)
    python 全栈开发,Day4(文件操作)
    python 全栈开发,Day3(集合,深浅copy)
    python 全栈开发,Day2(基础数据类型)
    python 全栈开发,Day1(python介绍,变量,if,while)
  • 原文地址:https://www.cnblogs.com/mingmingruyuedlut/p/3436803.html
Copyright © 2020-2023  润新知