根据其他值从 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,
};
我编写的代码不正确。它现在返回我所有财产的本金和货币。这些注释描述了出于理解目的应返回的内容。 该查询基本上应该根据
节点下的
节点返回价格和货币。不知道如何在这种情况下提出条件。
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 技术交流群。

发布评论
评论(2)
拔了角的鹿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("")
};
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果您首先找到包含
Principal
类型或Shipping
类型的元素,然后获取所需的值,将会有所帮助。我相信这就是你所追求的。只需记住包含命名空间
System.Xml.XPath
即可使其正常工作。It would help if you first find the elements that contained the
Principal
type orShipping
type first, then get the desired values. I believe this is what you're after.Just remember to include the namespace
System.Xml.XPath
for this to work.