使用LINQ查询我的XML仍然存在问题

时间:2010-01-30 19:09:20

标签: c# xml

所以我对此很接近,但是当我的ItemPrice Element(例如Tax)中的一个组件丢失时,我的整个Order / Line都没有返回。如果像“税”这样的组件有时会丢失,我会怎么做?

XML:

<?xml version="1.0" encoding="UTF-8"?>
<SettlementReport>
<Order>
  <AmazonOrderID>105-6982537-6258888</AmazonOrderID> 
  <ShipmentID>MyShipmentIDTest1234</ShipmentID> 
  <MarketplaceName>Amazon.com</MarketplaceName> 
    <Fulfillment>
      <MerchantFulfillmentID>MyTestFulFillID12345</MerchantFulfillmentID> 
      <PostedDate>2008-12-15T19:33:04+00:00</PostedDate> 
        <Item>
          <AmazonOrderItemCode>13350774331938</AmazonOrderItemCode> 
          <SKU>U1409</SKU> 
          <Quantity>1</Quantity> 
            <ItemPrice>
                <Component>
                      <Type>Principal</Type> 
                      <Amount currency="USD">0.15</Amount> 
                  </Component>
                <Component>
                      <Type>Tax</Type> 
                      <Amount currency="USD">0.02</Amount> 
                  </Component>
              </ItemPrice>
          </Item>
          <Item>
            <AmazonOrderItemCode>13350774331939</AmazonOrderItemCode> 
            <SKU>U14010</SKU> 
            <Quantity>2</Quantity> 
            <ItemPrice>
                <Component>
                      <Type>Principal</Type> 
                      <Amount currency="USD">0.30</Amount> 
                  </Component>
                <Component>
                      <Type>Tax</Type> 
                      <Amount currency="USD">0.04</Amount> 
                  </Component>
              </ItemPrice>
          </Item>
      </Fulfillment>
  </Order>
<Order>
  <AmazonOrderID>105-6982537-6259999</AmazonOrderID> 
  <ShipmentID>MyShipmentIDTest1234</ShipmentID> 
  <MarketplaceName>Amazon.com</MarketplaceName> 
    <Fulfillment>
      <MerchantFulfillmentID>MyTestFulFillID12345</MerchantFulfillmentID> 
      <PostedDate>2008-12-15T19:33:04+00:00</PostedDate> 
        <Item>
          <AmazonOrderItemCode>13350774331940</AmazonOrderItemCode> 
          <SKU>U1409</SKU> 
          <Quantity>1</Quantity> 
            <ItemPrice>
                <Component>
                      <Type>Principal</Type> 
                      <Amount currency="USD">0.40</Amount> 
                  </Component>
                <Component>
                      <Type>Tax</Type> 
                      <Amount currency="USD">0.15</Amount> 
                  </Component>
              </ItemPrice>
          </Item>
          <Item>
            <AmazonOrderItemCode>13350774331941</AmazonOrderItemCode> 
            <SKU>U14010</SKU> 
            <Quantity>2</Quantity> 
            <ItemPrice>
                <Component>
                      <Type>Principal</Type> 
                      <Amount currency="USD">0.50</Amount> 
                  </Component>
              </ItemPrice>
          </Item>
      </Fulfillment>
  </Order>    

我的代码:

XDocument customer = XDocument.Load(@"C:\LinqToXML.xml");

            var orders = from amznorders in customer.Root.Elements("Order")
                         from amznfulfill in amznorders.Elements("Fulfillment")
                         from amznitems in amznfulfill.Elements("Item")
                         from amznitemprc1 in amznitems.Elements("ItemPrice").Elements("Component")
                         from amznitemprc2 in amznitems.Elements("ItemPrice").Elements("Component")
                         let amznitemprinc = amznitemprc1.Element("Amount")
                         where (string)amznitemprc1.Element("Type") == "Principal"
                         let amznitemprtax = amznitemprc2.Element("Amount")
                         where (string)amznitemprc2.Element("Type") == "Tax"
                         select new
                         {
                             OrderNumber = (string)amznorders.Element("AmazonOrderID"),
                             ItemNumber = (string)amznitems.Element("AmazonOrderItemCode"),
                             Qty = amznitems == null ? "0" : (string)amznitems.Element("Quantity"),
                             PriceAmount = amznitemprinc == null ? String.Empty : (string)amznitemprinc,
                             TaxAmount = amznitemprtax  == null ? String.Empty : (string)amznitemprtax 
                         };


            foreach (var order in orders)
            {
                Console.WriteLine("Order: {0} ItemNumber: {1} QTY: {2}  {3}  {4}", order.OrderNumber, order.ItemNumber, order.Qty,order.PriceAmount,order.TaxAmount);

            }

2 个答案:

答案 0 :(得分:0)

这个怎么样:

var orders = from amznorders in customer.Root.Elements("Order")
             from amznfulfill in amznorders.Elements("Fulfillment")
             from amznitems in amznfulfill.Elements("Item")
             let amznitemprcs = amznitems.Elements("ItemPrice").Elements("Component").Select(element => new {
                 Type = element.Element("Type").Value,
                 Amount = element.Element("Amount").Value
             })
             select new
             {
                 OrderNumber = amznorders.Element("AmazonOrderID").Value,
                 ItemNumber = amznitems.Element("AmazonOrderItemCode").Value,
                 Qty = amznitems.Element("Quantity").Value,
                 PriceAmount = amznitemprcs.Where(x => x.Type == "Principal").Select(x => x.Amount).FirstOrDefault() ?? string.Empty,
                 TaxAmount = amznitemprcs.Where(x => x.Type == "Tax").Select(x => x.Amount).FirstOrDefault() ?? string.Empty,
             };

结果:

Order: 105-6982537-6258888 ItemNumber: 13350774331938 QTY: 1  0.15  0.02
Order: 105-6982537-6258888 ItemNumber: 13350774331939 QTY: 2  0.30  0.04
Order: 105-6982537-6259999 ItemNumber: 13350774331940 QTY: 1  0.40  0.15
Order: 105-6982537-6259999 ItemNumber: 13350774331941 QTY: 2  0.50

答案 1 :(得分:0)

和Linq一样棒,我不相信它是查询XML文档的最佳解决方案。这是一个使用Linq和XPath

组合的变体
var items = from li in doc.XPathSelectElements("/SettlementReport/Order/Fulfillment/Item")
       select new {
         OrderNumber = li.XPathSelectElement("../..").Element("AmazonOrderID").Value,
         ItemNumber = li.Element("AmazonOrderItemCode").Value,
         Quantity = li.Element("AmazonOrderItemCode").Value,
         PriceAmount = li.XPathSelectElement("ItemPrice/Component[Type='Principal']") != null ? li.XPathSelectElement("ItemPrice/Component[Type='Principal']").Element("Amount").Value : string.Empty,
         TaxAmount = li.XPathSelectElement("ItemPrice/Component[Type='Tax']") != null ? li.XPathSelectElement("ItemPrice/Component[Type='Tax']").Element("Amount").Value : string.Empty
      };