根据其他值从 XML 文档中选择某些值

发布于 11-19 04:21 字数 2360 浏览 1 评论 0原文

我有以下 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,
        };

我编写​​的代码不正确。它现在返回我所有财产的本金和货币。这些注释描述了出于理解目的应返回的内容。 该查询基本上应该根据 节点下的 节点返回价格和货币。不知道如何在这种情况下提出条件。

I have the following 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>

I need to get the Amounts for each type. Below is what I'm trying

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,
        };

The code I have written is incorrect. It returns me Principal Amount and Currency for all the properties right now. The comments describe what should be returned in it for understanding purpose.
The query should basically return me the Prices and Currency depending on the <Type> node under <Component> node. Not sure how to put a condition in this case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许久2024-11-26 04:21:04

如果您首先找到包含 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 即可使其正常工作。

It would help if you first find the elements that contained the Principal type or Shipping type first, then get the desired values. I believe this is what you're after.

// 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"),
            };

Just remember to include the namespace System.Xml.XPath for this to work.

拔了角的鹿2024-11-26 04:21:04

你只需要添加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("")
         };

you just have to add Where clause,

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("")
         };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文