我想检索产品信息并将其存储在我的集合IEnumerable中。但是,仅返回两种产品的信息。我尝试将代码从Element
更改为Elements
,但它只是引发了错误。
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>
主要代码
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; }
}
答案 0 :(得分:0)
xml中只有两个ListOrderItemsResult元素,因此您的方法应该只返回两个变量。
如果这不是问题,那么关于错误的更多信息就会很好。
答案 1 :(得分:0)
按如下方式更改您的Products方法。
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"),
};