c# - 使用 LINQ 查询我的 XML 时仍然有问题

标签 c# xml

所以我很接近这个,但是当我的 ItemPrice 元素(例如税收)下的组件之一丢失时,我的整个订单/行不会返回。如果有时缺少像“税收”这样的组件,我有什么想法吗?

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);

            }

最佳答案

这个怎么样:

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

关于c# - 使用 LINQ 查询我的 XML 时仍然有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2169054/

相关文章:

c# - 当需要在悬停时访问特定子类信息时,如何在 Unity C# 中向下转型?

c# - 将电子邮件(文本文件)的每个单词放入数组 C# 的最简单方法

javascript - 如何使用 AJAX 在 javascript 变量中发布数据

Python lxml XPath : preceding keyword does not give expected result

javascript - 使用 JavaScript 提取 XML 数据

xml - Microsoft Azure TTS 认知服务语音限制问题

c# - 检测给定字符串的地址类型

c# - 错误 : Not all code paths return a value

c# - 在 .NET List<T> 中将项目设置为 null 是否使其可用于垃圾收集,这是一个好主意吗?

xml - Prestashop 1.6 API 更新产品数量