Search code examples
c#xmlebay-api

Create XML DOC from API using foreach


I have an API call to eBay Trading API to GetOrders. Once I have my data I need to return it and also create a custom XML doc for a different system to use.

what I would like to get is any output like this for each order

<Order>
    <OrderNumber>123456789</OrderNumber>
    <OrderDate>2-Jul-2014 14:25:33</OrderDate>
    <Billing>
        <FullName>Full name</FullName>
        <Email>some@email.com</Email>
        <Phone>777-123-1234</Phone>
        <Address>
            <Street1>123 main sr</Street1>
            <City>Hillsville</City>
            <State>VA</State>
            <Code>24343</Code>
            <Country>US</Country>
        </Address>
    </Billing>
</Order>

Here is the api call I have right now.

using System;
using System.Web.Http;
using System.Xml;
using System.Xml.Linq;
using eBay.Service.Call;
using eBay.Service.Core.Sdk;
using eBay.Service.Core.Soap;

namespace SellerChain.Areas.eBayAPI.Controllers
{
    public class GetOrdersController : ApiController
    {
        public object Get()
        {
            var apiContext = token.ApiContext();

            var timeFilter = new TimeFilter
            {
                TimeFrom = Convert.ToDateTime("2014-07-01T16:15:34.868Z"),
                TimeTo = DateTime.UtcNow
            };

            const OrderStatusCodeType orderStatus = OrderStatusCodeType.Completed;
            const TradingRoleCodeType orderRole = TradingRoleCodeType.Seller;

            var apiCall = new GetOrdersCall(apiContext)
            {
                DetailLevelList = new DetailLevelCodeTypeCollection
                {
                    DetailLevelCodeType.ReturnSummary
                },
                Pagination = new PaginationType
                {
                    EntriesPerPage = 10,
                    PageNumber = 1,
                },
                IncludeFinalValueFee = true,
                CreateTimeFilter = timeFilter,
                OrderStatus = orderStatus,
                OrderRole = orderRole,


            };

            apiCall.GetOrders(
                timeFilter, 
                (TradingRoleCodeType)Enum.Parse(typeof(TradingRoleCodeType), orderRole.ToString()),
                (OrderStatusCodeType)Enum.Parse(typeof(OrderStatusCodeType), orderStatus.ToString())
                );

            return apiCall;

        }
    }
}

Solution

  • Try this

    var doc = new XDocument(
      new XElement("SEITOrders",
      from o in apiCall.ApiResponse.OrderArray.ToArray()
      select 
      new XElement("order", 
      new XElement("orderID", o.OrderID))));