Martin Morski

Martin Morski

  • NA
  • 5
  • 3.5k

Read from XML file and display

Jul 17 2014 6:56 PM
I am trying to read data from an XML file were the price is but the price get data from all nodes why? Plus it displays it two times?
 
i have created a button and a listbox when i press the button it should display like this for example
 
Milk 10
Bread 15
Butter 16
 
and so on.
 
Right now it displays all the values from the xml file( the prices) and also two times
 
Milk 101516 like this is it right now
 
Here is my code for my button
 
private void button1_Click(object sender, EventArgs e)
{


var xmlDoc = new XmlDocument();
xmlDoc.Load(@"c:\users\kalle\documents\visual studio 2013\Projects\Test\Test\prices.xml");

XmlNodeList xnListPerItem = xmlDoc.SelectNodes("/Prices");

if (xnListPerItem != null)
foreach (XmlNode xn in xnListPerItem)
{

string pricePerItem = xn["PricePerItem"].InnerText;
string pricePerKg = xn["PricePerKg"].InnerText;

listBox1.Items.Clear();

var food = new List<string>();

food.Add("Milk, Low fat, 1Liter" + pricePerItem);
food.Add("Butter" + pricePerItem);
food.Add("Bread" + pricePerItem);

food.Add("Apple, Jonagold" + pricePerKg);
food.Add("Chicken" + pricePerKg);
food.Add("Salad" + pricePerKg);

foreach (var vItem in food)
{
listBox1.Items.Add(vItem + "" + pricePerItem);
listBox1.Items.Add(vItem + "" + pricePerKg);
}


}

 
and this is my xml file
 
 <?xml version="1.0" encoding="utf-8" ?>
<Prices>
<PricePerItem>
<Item Name="Milk, Low fat, 1Liter">11.2</Item>
<Item Name="Butter">17</Item>
<Item Name="Bread">12.2</Item>
<Item Name="Cheese">15.5</Item>
</PricePerItem>
<PricePerKg>
<Item Name="Apple, Jonagold">13.4</Item>
<Item Name="Chicken">12.5</Item>
<Item Name="Salad">9.6</Item>
<Item Name="Fish, Salmon">14</Item>
</PricePerKg>
</Prices>
 
 
 

Answers (1)