zoukankan      html  css  js  c++  java
  • 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>

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

  • 相关阅读:
    聊聊ES6中的generator
    generator-yield到底是个啥
    jquery 常用方法中那些我不知道的事
    jquery 获取textarea文本值详解
    数组去重
    五指棋人机大战之ai篇
    五指棋人机大战之ui篇
    css控制背景图像不随滚动条的滚动而滚动
    用canvas画会旋转的伞
    CSS3 实现太极图案
  • 原文地址:https://www.cnblogs.com/mingmingruyuedlut/p/3436803.html
Copyright © 2011-2022 走看看