1

I want to retrieve the product info and store it in my collection IEnumerable. However, only the information for two products is being returned. I tried to change my code from Element to Elements but it just threw an error.

Orderlist.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListOrderItemsResponse
    xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
    <ListOrderItemsResult>
        <AmazonOrderId>002-1893166-3105064</AmazonOrderId>
        <OrderItems>
            <OrderItem>
                <ASIN>B00HUX1W1G</ASIN>
                <SellerSKU>NX-CHAG-UT0Q</SellerSKU>
                <OrderItemId>37783785306314</OrderItemId>
                <Title>Polyethylene Glycol (Peg) 200, ACS, Reagent Grade, One Quart Bottle</Title>
                <QuantityOrdered>1</QuantityOrdered>
                <QuantityShipped>0</QuantityShipped>
                <ItemPrice>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>25.00</Amount>
                </ItemPrice>
                <ShippingPrice>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>27.00</Amount>
                </ShippingPrice>
                <GiftWrapPrice>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </GiftWrapPrice>
                <ItemTax>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </ItemTax>
                <ShippingTax>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </ShippingTax>
                <GiftWrapTax>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </GiftWrapTax>
                <ShippingDiscount>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </ShippingDiscount>
                <PromotionDiscount>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </PromotionDiscount>
                <PromotionIds />
                <ConditionNote>Alliance Chemical</ConditionNote>
                <ConditionId>New</ConditionId>
                <ConditionSubtypeId>New</ConditionSubtypeId>
            </OrderItem>
        </OrderItems>
    </ListOrderItemsResult>
    <ListOrderItemsResult>
        <AmazonOrderId>102-5169016-4946643</AmazonOrderId>
        <OrderItems>
            <OrderItem>
                <ASIN>B00OKOU4GM</ASIN>
                <SellerSKU>35-9988-XJB8</SellerSKU>
                <OrderItemId>23504328079730</OrderItemId>
                <Title>Nitric Acid 65%, Acs, Reagent Grade, Quart(1 liter) Glass Bottle</Title>
                <QuantityOrdered>1</QuantityOrdered>
                <QuantityShipped>0</QuantityShipped>
                <ItemPrice>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>48.00</Amount>
                </ItemPrice>
                <ShippingPrice>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>27.00</Amount>
                </ShippingPrice>
                <GiftWrapPrice>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </GiftWrapPrice>
                <ItemTax>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </ItemTax>
                <ShippingTax>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </ShippingTax>
                <GiftWrapTax>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </GiftWrapTax>
                <ShippingDiscount>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </ShippingDiscount>
                <PromotionDiscount>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </PromotionDiscount>
                <PromotionIds />
                <ConditionId>New</ConditionId>
                <ConditionSubtypeId>New</ConditionSubtypeId>
            </OrderItem>
            <OrderItem>
                <ASIN>B00D6JGB2S</ASIN>
                <SellerSKU>DB-ZAYD-0QYZ</SellerSKU>
                <OrderItemId>58842277149658</OrderItemId>
                <Title>HYDROCHLORIC ACID 20 Be. Technical grade-One gallon bottle</Title>
                <QuantityOrdered>1</QuantityOrdered>
                <QuantityShipped>0</QuantityShipped>
                <ItemPrice>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>35.00</Amount>
                </ItemPrice>
                <ShippingPrice>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>27.00</Amount>
                </ShippingPrice>
                <GiftWrapPrice>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </GiftWrapPrice>
                <ItemTax>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </ItemTax>
                <ShippingTax>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </ShippingTax>
                <GiftWrapTax>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </GiftWrapTax>
                <ShippingDiscount>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </ShippingDiscount>
                <PromotionDiscount>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>0.00</Amount>
                </PromotionDiscount>
                <PromotionIds />
                <ConditionId>New</ConditionId>
                <ConditionSubtypeId>New</ConditionSubtypeId>
            </OrderItem>
        </OrderItems>
    </ListOrderItemsResult>
    <ResponseMetadata>
        <RequestId>b4d81fad-7d09-4e07-b166-e5638292d56d</RequestId>
    </ResponseMetadata>
</ListOrderItemsResponse>  

Code in Main

path = Path.Combine(Directory.GetCurrentDirectory(), "Orderlist.xml");
file = XDocument.Load(path);

xml = file.ToString();
doc = XDocument.Parse(xml);

var products = Products(doc);

private static IEnumerable<Address> Products(XContainer doc)
{
    XNamespace ns = "https://mws.amazonservices.com/Orders/2013-09-01";

    return from product in doc.Descendants(ns + "ListOrderItemsResult")
           select new Address
           {
               Title = (string)product.Element(ns + "OrderItems").Element(ns + "OrderItem").Element(ns + "Title"),  
               ItemPrice = (string)product.Element(ns + "OrderItems").Element(ns + "OrderItem").Element(ns + "ItemPrice").Element(ns + "Amount"),
               ShippingPrice = (string)product.Element(ns + "OrderItems").Element(ns + "OrderItem").Element(ns + "ShippingPrice").Element(ns + "Amount"),
               Quantity = (string)product.Element(ns + "OrderItems").Element(ns + "OrderItem").Element(ns + "QuantityOrdered"),
               P_OrderId = (string)product.Element(ns + "AmazonOrderId"),
           };
}


public class Address
{
    public string Name { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Title { get; set; }
    public string ItemPrice { get; set; }
    public string ShippingPrice { get; set; }
    public string Quantity { get; set; }
    public string P_OrderId { get; set; }
    public string S_OrderId { get; set; }
    public string PostalCode { get; set; }
}
1
  • What error was thrown? Commented Jul 23, 2015 at 17:07

2 Answers 2

0

Change your Products method as follows.

return from product in doc.Descendants(ns + "ListOrderItemsResult")
    from order in product.Element(ns + "OrderItems").Elements(ns + "OrderItem")
    select new Address
    {
       Title = (string)order.Element(ns + "Title"),
       ItemPrice = (string)order.Element(ns + "ItemPrice").Element(ns + "Amount"),
       ShippingPrice = (string)order.Element(ns + "ShippingPrice").Element(ns + "Amount"),
       Quantity = (string)order.Element(ns + "QuantityOrdered"),
       P_OrderId = (string)product.Element(ns + "AmazonOrderId"),
    };
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, this looks like what I want to do. I'll run it when I get the chance busy at work.
Works like a charm I think I am beginning to understand how LINQ queries work hah
0

You only have two ListOrderItemsResult elements in your xml, so your method should only be returning two variables.

If that's not the problem, some more information on what the errors were would be nice.

1 Comment

Ah! didn't notice that I need it to return three orders, going to give the @Sarathy code a run in a few. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.