Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i got a xml document im loading in C#, and im trying to get nodes by count. My problem is it only takes the last node in the loop. The nodes has an id called 2 but my count start at 0. so it jumbs over two nodes. The thing i want is input form value counting with nodeA["AddonPrice"].InnerText by id because i have multible nodes.

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("./xmlRe/" + filenDato + ".xml"));

int antalAdd0;
int ansver;
int pris;
int total = 0;

XmlNodeList nodeListADDon1 = doc.SelectNodes("TourInfo/AddOns/AddOn");
litTotal.Text = "";
string addon = "";
XmlAttribute xKey = doc.CreateAttribute("Id");
int count = 0;
int adID = 0;
xKey.Value = "addon" + adID;
addon = Request.Form["txtAddonAntal" + count];
antalAdd0 = int.Parse(addon.ToString());

foreach (XmlNode nodeA in nodeListADDon1)
{


nodeA.Attributes.Append(xKey);
pris = int.Parse(nodeA["AddonPrice"].InnerText);

ansver = antalAdd0 * pris;
litAdd.Text = "" + "" + "" + "" + "
" + nodeA["AddonText"].InnerText + "" + antalAdd0.ToString() + "Pris: " + ansver.ToString() + " kr

";

total += ansver;
litTotal.Text += total.ToString() + " kr";
Posted
Updated 23-Jun-15 3:14am
v3

1 solution

total += ansver; should be within the foreach loop, above the '}'


You have some counts with assignments in the loops such as ansver = antalAdd0 * pris;. Each loop will assign a number but nothing is added together.

if you want a sum then you will need a += within the loop somewhere

if you only use '=' within the loop then each loop will overwrite what the previous loop set it as.

You use depends on what exactly you are trying to do.

If this is still not helpful then please click the "Improve Question" button and expand on your issue. More that "i got a xml document im loading in C#, and im trying to get nodes by count. but it only selectin last node" would be helpful.

Explain what node you need, what value type it is and how it should be calculated.

It looks like you are very close to what you need
 
Share this answer
 
v2
Comments
Martin Lauritsen 23-Jun-15 8:14am    
thx m8 but when i do that, it does something funny, it counts the price from the last node that is 178 and writes 528 in total price. Sry about my english grammar :)
Andy Lanng 23-Jun-15 8:52am    
Please see my updated answer. You may have to add some further description of what you need.
Your English is fine. I've heard much worse locally, and I live here :P
Martin Lauritsen 23-Jun-15 8:16am    
if i remove the +, it counts allright but stil picking the last node
Martin Lauritsen 23-Jun-15 8:57am    
okay it make sense, what you are saying. Im trying to change it, or else i will update my question and be a bit more specific :)
Andy Lanng 23-Jun-15 9:16am    
Great. It's always better when it clicks for you :)

good luck ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900