C# DOM XML Parser displays 1 entry instead of 2 -
i have simple xml reader reads xml file dom approach. problem is, have 2 products in particular xml file displays second product (refer invoiceitem box). i'm pretty sure i've made stupid mistake somewhere don't see it.
source code: http://pastebin.com/rh1pf92n
xml:
<?xml version="1.0" encoding="utf-8" ?> <invoices> <invoice id="i1"> <invoicedate>21/06/2016</invoicedate> <sellerid>supp001</sellerid> <buyerid>wcs1810</buyerid> <orderid>o1</orderid> <invoiceitem> <product id="r1"> <productname>8gb ram king</productname> <description>8gb ram king brand</description> <capacity>8gb</capacity> <quantity>150</quantity> <unitprice>100</unitprice> </product> <product id="r2"> <productname>4gb ram king</productname> <description>4gb ram king brand</description> <capacity>4gb</capacity> <quantity>100</quantity> <unitprice>50</unitprice> </product> </invoiceitem> <shippingcharges>5</shippingcharges> <invoicetotal>205</invoicetotal> </invoice> </invoices>
in foreach (xmlnode node2 in productlist)
iterate through both products code populates invoiceitem
, adds invoiceitems
outside of foreach loop, when foreach loop finishes adding last element loop invoiceitems
. put code inside product foreach
invoiceitem = new invoiceitem(); invoiceitem.invoiceid = node.attributes[0].value; invoiceitem.productid = prodid; invoiceitem.description = description; invoiceitem.capacity = capacity; invoiceitem.quantity = qty; invoiceitem.unitprice = unitpx; invoiceitem.totalprice = (qty * unitpx); invoiceitems.add(invoiceitem);
Comments
Post a Comment