简体   繁体   English

帮助使用Linq查询XML

[英]Help using Linq to Query XML

I have this XML that I want to be able to pull out the Order#, Item(s), Qty, ItemPrice (Principal, Tax, could be others as well). 我有这个XML,我希望能够提取Order#,Item(s),Qty,ItemPrice(Principal,Tax,也可以是其他人)。 Here is the XML (see below). 这是XML(见下文)。 What I am having problems with is wiht the ItemPrice. 我遇到的问题是与ItemPrice有关。 With in it you can 0 to Many Price Components, Tax, Principal Etc. How can I pull those out into a single line output? 有了它,你可以0到很多价格组件,税,主要等。我怎样才能把它们拉成单行输出?

Right Now I can get orderNumber, ItemNumber, Qty...but when i pull the Price I get a line for tax and prinicpal. 现在我可以得到orderNumber,ItemNumber,Qty ......但是当我拉价格时,我得到一条税和原则线。

One other question is what happens if in some instances Tax isn't there? 另一个问题是如果在某些情况下税收不存在会发生什么? I will get a null reference won't I? 我会得到一个空引用不是吗? I want to try and handle those and just substitute 0. Any suggestions are greatly appreicated. 我想尝试处理那些而只是替换0.任何建议都非常有用。

XML: 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>
</SettlementReport>

My Code: 我的代码:

            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 amznitemprc in amznitems.Elements("ItemPrice").Elements("Component")
                             select new
                                        {
                                            OrderNumber = (string)amznorders.Element("AmazonOrderID"),
                                            ItemNumber = (string)amznitems.Element("AmazonOrderItemCode"),
                                            QTY = (string)amznitems.Element("Quantity"),
                                            //This is where I need help...if type = Principal set PriceAmount else Set PriceTax?//
                                            PriceAmount = (string)amznitemprc.Element("Amount")
                                            //Address1 = (string)address1.Element("Address1")
                                     };

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

            }

Doable, though admittedly a bit awkward. 虽然可行,但确实有点尴尬。 I'd probably use a pair of let variables hold the sub-components I want (assuming you only want those two specific values). 我可能会使用一对let变量来保存我想要的子组件(假设你只需要那两个特定的值)。

Edited to remove the lets due to problem with the Where clause. 由于Where子句的问题而编辑删除了let。 I don't think there's a problem with the Where in the select, but it may have the same problem. 我不认为选择中的Where有问题,但它可能有同样的问题。

var orders = from amznorders in customer.Root.Elements("Order")    
             from amznfulfill in amznorders.Elements("Fulfillment")    
             from amznitems in amznfulfill.Elements("Item")    
             from amznitemprc in amznitems.Elements("ItemPrice").Elements("Component")    
             select new    
                         {    
                                OrderNumber = (string)amznorders.Element("AmazonOrderID"),    
                                ItemNumber = (string)amznitems.Element("AmazonOrderItemCode"),    
                                Qty = (string)amznitems.Element("Quantity"),
                                PriceAmount = amznitemprc.Where(xe => xe.Element("Type").Value == "Principal").FirstOrDefault().Value,
                                TaxAmount = amxnitemprc.Where(xe => xe.Element("Type").Value == "Tax").FirstOrDefault() == null ? string.Empty : amxnitemprc.Where(xe => xe.Element("Type").Value == "Tax").FirstOrDefault(),
                                TotalItemPrice = amznitemprc.Sum(xe => decimal.Parse(xe.Element("Amount").Value)).ToString(),
                                //Address1 = (string)address1.Element("Address1")    
                         };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM