我有以下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>
节点下。不知道如何在这种情况下放置一个条件。
我认为你的班级结构留下了改进的空间。用'Value'和'Currency'属性创建一个类'Amount'。用'Tax','ShipTax'等属性创建一个'Item'类。 – Sjoerd
你将不得不改变你的问题。我不知道你在问什么,我怀疑其他人会有同样的感受。 (你的代码甚至不会编译) –
@Sjoerd - 我从我的应用程序中只能使用第三方应用程序获取XML。 @Jeff Mercado - 对不起。让我看看我该如何做得更好。 –