解析亚马逊订单响应xml

时间:2014-07-31 12:04:56

标签: c# xml parsing linq-to-xml amazon

 <ListOrdersResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
 <ListOrdersResult>
<NextToken>xxxxxxxx</NextToken>
<CreatedBefore>07/31/2014 15:35:36</CreatedBefore>
<Orders>
  <Order>
    <AmazonOrderId>xxxxxx</AmazonOrderId>
    <SellerOrderId>xxxxxx</SellerOrderId>
    <PurchaseDate>07/21/2014 11:11:21</PurchaseDate>
    <LastUpdateDate>07/21/2014 21:41:23</LastUpdateDate>
    <OrderStatus>Shipped</OrderStatus>
    <FulfillmentChannel>AFN</FulfillmentChannel>
    <SalesChannel>Amazon.in</SalesChannel>
    <ShipServiceLevel>Standard</ShipServiceLevel>
    <ShippingAddress>
      <Name>xxxx</Name>
      <AddressLine1>xxxx</AddressLine1>
      <AddressLine2>xxxxx</AddressLine2>
      <City>xxxx</City>
      <StateOrRegion>xxx</StateOrRegion>
      <PostalCode>xxx</PostalCode>
      <CountryCode>IN</CountryCode>
      <Phone>xxxxxx</Phone>
    </ShippingAddress>
    <OrderTotal>
      <CurrencyCode>INR</CurrencyCode>
      <Amount>355.00</Amount>
    </OrderTotal>
    <NumberOfItemsShipped>1</NumberOfItemsShipped>
    <NumberOfItemsUnshipped>0</NumberOfItemsUnshipped>
    <PaymentExecutionDetail />
    <PaymentMethod>Other</PaymentMethod>
    <MarketplaceId>xxxxx</MarketplaceId>
    <BuyerEmail>xxxxx</BuyerEmail>
    <BuyerName>xxxxx</BuyerName>
    <ShipmentServiceLevelCategory>Standard</ShipmentServiceLevelCategory>
    <OrderType>StandardOrder</OrderType>
    <EarliestShipDate>07/21/2014 21:23:06</EarliestShipDate>
    <LatestShipDate>07/21/2014 21:23:06</LatestShipDate>
  </Order>
 </ListOrdersResult>
 <ResponseMetadata>
<RequestId>16a8a438-f774-4b17-824f-951a4cde7ddc</RequestId>
</ResponseMetadata>
</ListOrdersResponse>

我正在尝试解析上面提到的Response。

 XDocument doc = XDocument.Parse(responseXml);
            XNamespace ns = "https://mws.amazonservices.com/Orders/2013-09-01";
            var data = (from attrib in doc.Descendants(ns + "Order")
                       select new FormsPersistence
                       {

                           AmazonOrderID = (string)attrib.Element(ns+"AmazonOrderID").Value ?? string.Empty,
                           //MerchantOrderID = (string)attrib.Element(ns + "MerchantOrderID").Value ?? string.Empty,
                           //PurchaseDate = (string)attrib.Element(ns + "PurchaseDate").Value ?? string.Empty,
                           //OrderStatus = (string)attrib.Element(ns + "OrderStatus").Value ?? string.Empty,
                           //ASIN = (string)attrib.Element(ns + "OrderItem").Element(ns + "ASIN").Value ?? string.Empty,
                           //SKU = (string)attrib.Element(ns + "OrderItem").Element(ns + "SKU").Value ?? string.Empty,
                          // ItemStatus = (string)attrib.Element(ns + "OrderItem").Element(ns + "ItemStatus").Value ?? string.Empty,
                           //ProductName = (string)attrib.Element(ns + "OrderItem").Element(ns + "ProductName").Value ?? string.Empty,
                           //Quantity = (string)attrib.Element(ns + "OrderItem").Element(ns + "Quantity").Value ?? string.Empty,

                           //Amount = (string)attrib.Element(ns + "OrderItem").Element(ns + "ItemPrice") == null ? string.Empty : (string)attrib.Element(ns + "OrderItem").Element(ns + "ItemPrice").Element(ns + "Component").Element(ns + "Amount").Value,

                       }).ToList();

我得到的参考没有设置对象错误.... 我评论过每一件事只是为了测试它。即使是第一个元素它的抛出异常。 我尝试了一切。如果有人能指出我正确的方向,我将不胜感激。

FormsPersistence是一个类

class FormsPersistence
{

    public string AmazonOrderID { get; set; }
    public string MerchantOrderID { get; set; }
    public string PurchaseDate { get; set; }
    public string OrderStatus { get; set; }
    public string ASIN { get; set; }
    public string SKU { get; set; }
    public string ItemStatus { get; set; }
    public string ProductName { get; set; }
    public string Quantity { get; set; }
    public string Amount { get; set; }


}

1 个答案:

答案 0 :(得分:0)

我不是在检查整个房产,但是对于第一个房产,它只是一个不同的情况。请注意,XML节点名称区分大小写,因此AmazonOrderID应该是AmazonOrderId

var data = (from attrib in doc.Descendants(ns + "Order")
            select new
            {

                AmazonOrderID = (string)attrib.Element(ns + "AmazonOrderId"),
                .....
                .....

            }).ToList();

因此,第一个建议是更仔细地查看您的代码。另一个建议如我的评论中所述,只需将XElement转换为合适的值而不是获取XElement.Value属性。

更新:

好的,我看过整个房产。 XML示例中没有名为MerchantOrderIDOrderItem的元素,因此代码中试图访问这些元素的所有行肯定会触发异常。