C# Linq XML get text from multiple elements with the same name -
i want retrieve product info , store in collection ienumerable. however, information 2 products being returned. tried change code element
elements
threw error.
orderlist.xml:
<?xml version="1.0" encoding="utf-8"?> <listorderitemsresponse xmlns="https://mws.amazonservices.com/orders/2013-09-01"> <listorderitemsresult> <amazonorderid>002-1893166-3105064</amazonorderid> <orderitems> <orderitem> <asin>b00hux1w1g</asin> <sellersku>nx-chag-ut0q</sellersku> <orderitemid>37783785306314</orderitemid> <title>polyethylene glycol (peg) 200, acs, reagent grade, 1 quart bottle</title> <quantityordered>1</quantityordered> <quantityshipped>0</quantityshipped> <itemprice> <currencycode>usd</currencycode> <amount>25.00</amount> </itemprice> <shippingprice> <currencycode>usd</currencycode> <amount>27.00</amount> </shippingprice> <giftwrapprice> <currencycode>usd</currencycode> <amount>0.00</amount> </giftwrapprice> <itemtax> <currencycode>usd</currencycode> <amount>0.00</amount> </itemtax> <shippingtax> <currencycode>usd</currencycode> <amount>0.00</amount> </shippingtax> <giftwraptax> <currencycode>usd</currencycode> <amount>0.00</amount> </giftwraptax> <shippingdiscount> <currencycode>usd</currencycode> <amount>0.00</amount> </shippingdiscount> <promotiondiscount> <currencycode>usd</currencycode> <amount>0.00</amount> </promotiondiscount> <promotionids /> <conditionnote>alliance chemical</conditionnote> <conditionid>new</conditionid> <conditionsubtypeid>new</conditionsubtypeid> </orderitem> </orderitems> </listorderitemsresult> <listorderitemsresult> <amazonorderid>102-5169016-4946643</amazonorderid> <orderitems> <orderitem> <asin>b00okou4gm</asin> <sellersku>35-9988-xjb8</sellersku> <orderitemid>23504328079730</orderitemid> <title>nitric acid 65%, acs, reagent grade, quart(1 liter) glass bottle</title> <quantityordered>1</quantityordered> <quantityshipped>0</quantityshipped> <itemprice> <currencycode>usd</currencycode> <amount>48.00</amount> </itemprice> <shippingprice> <currencycode>usd</currencycode> <amount>27.00</amount> </shippingprice> <giftwrapprice> <currencycode>usd</currencycode> <amount>0.00</amount> </giftwrapprice> <itemtax> <currencycode>usd</currencycode> <amount>0.00</amount> </itemtax> <shippingtax> <currencycode>usd</currencycode> <amount>0.00</amount> </shippingtax> <giftwraptax> <currencycode>usd</currencycode> <amount>0.00</amount> </giftwraptax> <shippingdiscount> <currencycode>usd</currencycode> <amount>0.00</amount> </shippingdiscount> <promotiondiscount> <currencycode>usd</currencycode> <amount>0.00</amount> </promotiondiscount> <promotionids /> <conditionid>new</conditionid> <conditionsubtypeid>new</conditionsubtypeid> </orderitem> <orderitem> <asin>b00d6jgb2s</asin> <sellersku>db-zayd-0qyz</sellersku> <orderitemid>58842277149658</orderitemid> <title>hydrochloric acid 20 be. technical grade-one gallon bottle</title> <quantityordered>1</quantityordered> <quantityshipped>0</quantityshipped> <itemprice> <currencycode>usd</currencycode> <amount>35.00</amount> </itemprice> <shippingprice> <currencycode>usd</currencycode> <amount>27.00</amount> </shippingprice> <giftwrapprice> <currencycode>usd</currencycode> <amount>0.00</amount> </giftwrapprice> <itemtax> <currencycode>usd</currencycode> <amount>0.00</amount> </itemtax> <shippingtax> <currencycode>usd</currencycode> <amount>0.00</amount> </shippingtax> <giftwraptax> <currencycode>usd</currencycode> <amount>0.00</amount> </giftwraptax> <shippingdiscount> <currencycode>usd</currencycode> <amount>0.00</amount> </shippingdiscount> <promotiondiscount> <currencycode>usd</currencycode> <amount>0.00</amount> </promotiondiscount> <promotionids /> <conditionid>new</conditionid> <conditionsubtypeid>new</conditionsubtypeid> </orderitem> </orderitems> </listorderitemsresult> <responsemetadata> <requestid>b4d81fad-7d09-4e07-b166-e5638292d56d</requestid> </responsemetadata> </listorderitemsresponse>
code in main
path = path.combine(directory.getcurrentdirectory(), "orderlist.xml"); file = xdocument.load(path); xml = file.tostring(); doc = xdocument.parse(xml); var products = products(doc); private static ienumerable<address> products(xcontainer doc) { xnamespace ns = "https://mws.amazonservices.com/orders/2013-09-01"; return product in doc.descendants(ns + "listorderitemsresult") select new address { title = (string)product.element(ns + "orderitems").element(ns + "orderitem").element(ns + "title"), itemprice = (string)product.element(ns + "orderitems").element(ns + "orderitem").element(ns + "itemprice").element(ns + "amount"), shippingprice = (string)product.element(ns + "orderitems").element(ns + "orderitem").element(ns + "shippingprice").element(ns + "amount"), quantity = (string)product.element(ns + "orderitems").element(ns + "orderitem").element(ns + "quantityordered"), p_orderid = (string)product.element(ns + "amazonorderid"), }; } public class address { public string name { get; set; } public string addressline1 { get; set; } public string addressline2 { get; set; } public string city { get; set; } public string state { get; set; } public string title { get; set; } public string itemprice { get; set; } public string shippingprice { get; set; } public string quantity { get; set; } public string p_orderid { get; set; } public string s_orderid { get; set; } public string postalcode { get; set; } }
change products method follows.
return product in doc.descendants(ns + "listorderitemsresult") order in product.element(ns + "orderitems").elements(ns + "orderitem") select new address { title = (string)order.element(ns + "title"), itemprice = (string)order.element(ns + "itemprice").element(ns + "amount"), shippingprice = (string)order.element(ns + "shippingprice").element(ns + "amount"), quantity = (string)order.element(ns + "quantityordered"), p_orderid = (string)product.element(ns + "amazonorderid"), };
Comments
Post a Comment