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.
It would help if you first find the elements that contained the
Principaltype orShippingtype first, then get the desired values. I believe this is what you’re after.Just remember to include the namespace
System.Xml.XPathfor this to work.