Selecting certain values from an XML document depending on other values
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")
开发者_StackOverflow .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 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.
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("")
};
精彩评论