2011-07-11 188 views
0

我有以下XML从XML文档依赖于其他值

<OrderReport> 
    <Item> 
    <Promotion>  
     <Component> 
     <Type>Principal</Type> 
     <Amount currency="USD">-0.25</Amount> 
     </Component> 
     <Component> 
     <Type>Shipping</Type> 
     <Amount currency="USD">0.00</Amount> 
     </Component> 
    </Promotion> 
    </Item> 
</OrderReport> 

我需要为每种类型的金额选择特定的值。以下是我想要的

var q = from orders in xDoc.Descendants("OrderReport")  
     select new 
     { 
      //This should return me the Principal Amount 
      ItemDiscountAmount = orders.Element("Item") 
             .Element("Promotion") 
             .Element("Component") 
             .Element("Amount") 
             .Value, 
      //This should return me the Principal Currency 
      ItemDiscountCurrency = orders.Element("Item") 
             .Element("Promotion") 
             .Element("Component") 
             .Element("Amount") 
             .Attribute("currency") 
             .Value, 

      //This should return me the Shipping Amount 
      ShipDiscountAmount = orders.Element("Item") 
             .Element("Promotion") 
             .Element("Component") 
             .Element("Amount") 
             .Value, 
      //This should return me the Shipping Currency       
      ShipDiscountCurrency = orders.Element("Item") 
             .Element("Promotion") 
             .Element("Component") 
             .Element("Amount") 
             .Attribute("currency") 
             .Value, 
     }; 

我写的代码不正确。它现在返回所有物业的本金和货币。评论描述了为了理解目的应该返回的内容。 该查询应基本上返回我的价格和货币取决于<Type>节点<Component>节点下。不知道如何在这种情况下放置一个条件。

+1

我认为你的班级结构留下了改进的空间。用'Value'和'Currency'属性创建一个类'Amount'。用'Tax','ShipTax'等属性创建一个'Item'类。 – Sjoerd

+1

你将不得不改变你的问题。我不知道你在问什么,我怀疑其他人会有同样的感受。 (你的代码甚至不会编译) –

+0

@Sjoerd - 我从我的应用程序中只能使用第三方应用程序获取XML。 @Jeff Mercado - 对不起。让我看看我该如何做得更好。 –

回答

0

这将帮助,如果你第一次发现含有Principal类型或Shipping型的第一要素,然后得到所需的值。我相信这就是你所追求的。

// using an XPATH will make this nicer to express 
var query = from report in xDoc.Descendants("OrderReport") 
      let principalAmount = report.XPathSelectElement("./*/*/Component[Type='Principal']/Amount") 
      let shippingAmount = report.XPathSelectElement("./*/*/Component[Type='Shipping']/Amount") 
      select new 
      { 
       ItemDiscountAmount = (decimal)principalAmount, 
       ItemDiscountCurrency = (string)principalAmount.Attribute("currency"), 
       ShipDiscountAmount = (decimal)shippingAmount, 
       ShipDiscountCurrency = (string)shippingAmount.Attribute("currency"), 
      }; 

只记得包括名称空间System.Xml.XPath这个工作。

+0

太棒了。工作就像一个魅力:)非常感谢 –

0

,你只需要Where子句添加,

var q = from orders in xdoc1.Descendants("OrderReport") 
     join ItemPrices in xdoc1.Descendants 
     where orders.Component.Type == "Principal" 
     select new 
     { 
      OrderId = orders.Element("OrderID"), 
      City = orders.Element("City"), 
      CountryRegion = orders.Element("CountryCode"), 
      State = orders.Element("StateOrRegion"), 
      Street1 = orders.Element("AddressFieldOne"), 
      Telephone = orders.Element("PhoneNumber"), 
      ZipCode = orders.Element("PostalCode"), 
      Name = orders.Element("Name"), 
      OrderDate = orders.Element("OrderDate"), 
      ShipMethod = orders.Element("FulfillmentMethod"), 
      ItemCode = orders.Element("AmazonOrderItemCode"), 
      SKU = orders.Element("SKU"), 
      QtyOrdered = orders.Element(""), 
      ItemTaxAmount = orders.Element(""), 
      ItemTaxCurrency = orders.Element(""), 
      ItemShipTaxAmount = orders.Element(""), 
      ItemShipTaxCurrency = orders.Element(""), 
      ItemTotalAmount = orders.Element(""), 
      ItemTotalCurrency = orders.Element(""), 
      ItemUnitPrice = orders.Element(""), 
      ItemCommissionAmount = orders.Element(""), 
      ItemCommissionCurrency = orders.Element("") 
     }; 
+0

感谢亚历山大, 但使用where子句我只会得到本金。我无法获得单一选择中的所有金额吗?如果您看到我拥有“委托人”,“税金”,“我的选择”中的所有金额。 –

+0

你可以用AND运算符在where子句中扩展你。Principal && Tax && Ship –

+0

你现在可以检查它。我编辑了这个问题,使其更加具体到问题领域。 –