gpt4 book ai didi

c# - 帮助使用 Linq 查询 XML

转载 作者:数据小太阳 更新时间:2023-10-29 02:46:50 27 4
gpt4 key购买 nike

我有这个 XML,我希望能够提取订单号、项目、数量、项目价格(本金、税金,也可以是其他)。这是 XML(见下文)。我遇到的问题是 ItemPrice。有了它,您可以从 0 到许多价格组成部分、税收、本金等。我怎样才能将它们拉出到单行输出中?

现在我可以获得订单号、商品号、数量...但是当我提取价格时,我会得到一条税和原则。

另一个问题是,如果在某些情况下没有税收会怎样?我会得到一个空引用,不是吗?我想尝试处理这些并替换为 0。非常感谢任何建议。

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>

我的代码:

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

}

最佳答案

可行,但确实有点尴尬。我可能会使用一对 let 变量来保存我想要的子组件(假设您只需要这两个特定值)。

由于 Where 子句的问题,编辑删除了 lets。我认为选择中的 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")
};

关于c# - 帮助使用 Linq 查询 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2165840/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com