Hi everyone,
I am trying to parse the fallowing XML file in C#:
<?xml version="1.0" encoding="utf-8" ?><orders><order><order_date>1/14/2014</order_date><customer_id>001</customer_id><product><product_id>sh_s</product_id><product_qt>8</product_qt></product><product><product_id>sh_m</product_id><product_qt>2</product_qt></product><product><product_id>pa_32s</product_id><product_qt>4</product_qt></product><product><product_id>ja_l</product_id><product_qt>1</product_qt></product></order><order><order_date>1/14/2014</order_date><customer_id>002</customer_id><product><product_id>sh_s</product_id><product_qt>10</product_qt></product><product><product_id>sh_m</product_id><product_qt>1</product_qt></product><product><product_id>pa_32s</product_id><product_qt>3</product_qt></product><product><product_id>ja_l</product_id><product_qt>5</product_qt></product></order><order><order_date>1/14/2014</order_date><customer_id>003</customer_id><product><product_id>sh_s</product_id><product_qt>1</product_qt></product><product><product_id>sh_m</product_id><product_qt>2</product_qt></product><product><product_id>pa_32s</product_id><product_qt>4</product_qt></product><product><product_id>ja_l</product_id><product_qt>7</product_qt></product></order></orders>
I have the following C# Code:
XmlDocument doc = new XmlDocument(); doc.Load(@"C:\Users\Rafael\Desktop\order.xml"); foreach (XmlNode xn in doc.SelectNodes("/orders/order")) { string orderDate = xn["order_date"].InnerText; string customer = xn["customer_id"].InnerText; int customerID = Int32.Parse(customer);//convert to integer MessageBox.Show(" | " + orderDate + " | " + customer + " | "); foreach (XmlNode item in doc.SelectNodes("/orders/order/product")) { string productID = item["product_id"].InnerText; string productQT = item["product_qt"].InnerText; int product_qt = Int32.Parse(productQT);//convert to integer MessageBox.Show(" | " + productID + " | " + productQT + " | "); }// Product foreach //Acknowledgement of storage //MessageBox.Show("Data stored successfuly!"); }// Order foreach
For each order I want to get the Date, Customer, and all products associated with it.
Currently the code gets the date and customer of the first order, and then gets the product Ids of ALL products in ALL orders and then repeats the process for each order.
E.G. > for the first <order> I get the date, customer ID, product ID, and additional unwanted product IDs from all the <order>s below.
Do you have any suggestion on how to fix this?
Thank you