<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>
I am trying to parse above mentioned 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();
I am getting reference not set on an object error.... i have commented every thing just to test it out.even for first element its throwing exception. I have tried everything. If someone could point me in right direction i will be grateful.
FormsPersistence is a class
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; }
}
I'm not checking the entire property, but for the first it is only a matter of different case. Note that XML node name is case sensitive, so AmazonOrderID
should've been AmazonOrderId
:
var data = (from attrib in doc.Descendants(ns + "Order")
select new
{
AmazonOrderID = (string)attrib.Element(ns + "AmazonOrderId"),
.....
.....
}).ToList();
So the first suggestion is look at your code more closely. Another suggestion is as stated in my comment, simply cast the XElement
to suitable value instead of getting XElement.Value
property.
UPDATE :
Okay, I have seen the entire property. There is no such element named MerchantOrderID
and OrderItem
in the XML sample, so all lines in your code trying to access those elements will definately trigger exception.