LINQ to XML嵌套元素

时间:2017-10-24 14:51:49

标签: c# xml linq

我可以读取XML并返回Order Element。

我还需要返回ShippingAddress,它嵌套在Order Element中。 (名称,AddressLine1,City,PostalCode,CountryCode,Phone)同时来自XML Response。

请有人告诉我如何同时联系ShippingAddress。

我目前的代码是

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

        return from address in doc.Descendants(ns + "Order")
               select new FormsPersistence
               {
                   AmazonOrderId = (string)address.Element(ns + "AmazonOrderId").Value ?? string.Empty,
                   PurchaseDate = (string)address.Element(ns + "PurchaseDate").Value ?? string.Empty,
                   BuyerName = (string)address.Element(ns + "BuyerName").Value ?? string.Empty,
                   BuyerEmail = (string)address.Element(ns + "BuyerEmail").Value ?? string.Empty,
                   OrderStatus = (string)address.Element(ns + "OrderStatus").Value ?? string.Empty,
               };
    }

    private static void OrderDetails(IEnumerable<FormsPersistence> addresses)
    {


        foreach (var address in addresses)
        {
            Console.WriteLine("---");
            Console.WriteLine(address.AmazonOrderId);
            Console.WriteLine(address.PurchaseDate);
            Console.WriteLine(address.BuyerName);
            Console.WriteLine(address.BuyerEmail);
            Console.WriteLine(address.OrderStatus);
            Console.WriteLine("---");
        }
    }

XML响应是

<ListOrdersResult>
<CreatedBefore>10/23/2017 14:09:56</CreatedBefore>
<Orders>
  <Order>
    <AmazonOrderId>026-000000-000000</AmazonOrderId>
    <PurchaseDate>10/20/2017 13:45:17</PurchaseDate>
    <LastUpdateDate>10/20/2017 14:16:38</LastUpdateDate>
    <OrderStatus>Unshipped</OrderStatus>
    <FulfillmentChannel>MFN</FulfillmentChannel>
    <SalesChannel>Amazon.co.uk</SalesChannel>
    <ShipServiceLevel>Std UK Dom_1</ShipServiceLevel>
    <ShippingAddress>
      <Name>kkkkk kkkk</Name>
      <AddressLine1>22 kkkkk road</AddressLine1>
      <City>London</City>
      <PostalCode>SW14 JBD</PostalCode>
      <CountryCode>GB</CountryCode>
      <Phone>0000000000</Phone>
    </ShippingAddress>
    <OrderTotal>
      <CurrencyCode>GBP</CurrencyCode>
      <Amount>46.00</Amount>
    </OrderTotal>
    <NumberOfItemsShipped>0</NumberOfItemsShipped>
    <NumberOfItemsUnshipped>6</NumberOfItemsUnshipped>
    <PaymentExecutionDetail />
    <PaymentMethod>Other</PaymentMethod>
    <PaymentMethodDetails>
      <PaymentMethodDetail>Standard</PaymentMethodDetail>
    </PaymentMethodDetails>
    <MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
    <BuyerEmail>kkkkk@kkkkk.amazon.co.uk</BuyerEmail>
    <BuyerName>kkk akkkk</BuyerName>
    <ShipmentServiceLevelCategory>Standard</ShipmentServiceLevelCategory>
    <ShippedByAmazonTFM>False</ShippedByAmazonTFM>
    <OrderType>StandardOrder</OrderType>
    <EarliestShipDate>10/23/2017 00:00:00</EarliestShipDate>
    <LatestShipDate>10/24/2017 23:59:59</LatestShipDate>
    <EarliestDeliveryDate>10/25/2017 00:00:00</EarliestDeliveryDate>
    <LatestDeliveryDate>10/27/2017 23:59:59</LatestDeliveryDate>
    <IsBusinessOrder>False</IsBusinessOrder>
    <IsPrime>False</IsPrime>
    <IsPremiumOrder>False</IsPremiumOrder>
    <IsReplacementOrder>False</IsReplacementOrder>
  </Order>
</Orders>

班级档案

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 BuyerName { get; set; }
    public string BuyerEmail { get; set; }
    public string Name { get; set; }
    public string AddressLine1 { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
    public string CountryCode { get; set; }
    public string Phone { get; set; }
    public string ShippingAddress { get; set; }
}

我加了

class ShippingAddress
{
    public string Name { get; set; }
    public string AddressLine { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
    public string CountryCode { get; set; }
    public string Phone { get; set; }
  }

2 个答案:

答案 0 :(得分:1)

假设FormsPersistence具有保存该属性的属性,您可以执行以下操作:

  return from address in doc.Descendants(ns + "Order")
         let shipping=address.Element(ns+"ShippingAddress")
         select new FormsPersistence
           {
               AmazonOrderId = (string)address.Element(ns + "AmazonOrderId").Value ?? string.Empty,
               PurchaseDate = (string)address.Element(ns + "PurchaseDate").Value ?? string.Empty,
               BuyerName = (string)address.Element(ns + "BuyerName").Value ?? string.Empty,
               BuyerEmail = (string)address.Element(ns + "BuyerEmail").Value ?? string.Empty,
               OrderStatus = (string)address.Element(ns + "OrderStatus").Value ?? string.Empty,
               ShippingAddress= new ShippingAddress{Name=(string)shipping.Element(ns+"Name"),
                                                    AddressLine=(string)shipping.Element(ns+"AddressLine1"),
                                                   }
           };

您需要使用Element方法,它允许您获取当前节点的内部元素,并且在let子句的帮助下,您可以保存结果以便稍后在投影中使用它正如我在上面的代码中所示。

答案 1 :(得分:0)

诀窍是使用Elements然后使用FirstOrDefault()

return doc.Descendants(ns + "Order").Select(address => new FormsPersistence()
               {
                   AmazonOrderId = (string)address.Element(ns + "AmazonOrderId").Value ?? string.Empty,
                   PurchaseDate = (string)address.Element(ns + "PurchaseDate").Value ?? string.Empty,
                   BuyerName = (string)address.Element(ns + "BuyerName").Value ?? string.Empty,
                   BuyerEmail = (string)address.Element(ns + "BuyerEmail").Value ?? string.Empty,
                   OrderStatus = (string)address.Element(ns + "OrderStatus").Value ?? string.Empty,
                   ShippingAddress = address.Elements(ns + "ShippingAddress").Select(shipping => new {
                        name = (string)shipping.Element(ns + "Name")
                   }).FirstOrDefault() 
               }).ToList();