Xml node reading for each loop

Ebikeneser picture Ebikeneser · Jan 10, 2011 · Viewed 74.3k times · Source

I am trying to loop through an Xml file and display the value for account in a message.

XmlNodeList nodeList = testDoc.SelectNodes("/details/row/var");
foreach (XmlNode no in nodeList)
{
   XmlNode node = testDoc.SelectSingleNode("/details/row/var[@name='account']");
   test.actual = node.Attributes["value"].Value;

   MessageBox.Show(test.account);
 }

The message box is currently displaying the first record repeatidly, how can I get to the next record?

Thanks for your input in advance.

Answer

Chris Dickson picture Chris Dickson · Jan 10, 2011

You are repeatedly assigning node with the same element from testDoc. It is not clear what test.account is (perhaps a mistype for test.actual)?

no is the variable which will iterate the contents of nodeList - I imagine you intended to use that.

EDIT following edit of OP Now you've shown us what nodeList is, I suspect you want to do something like this instead :

XmlNodeList nodeList = testDoc.SelectNodes("/details/row/var[@name='account']"); 
foreach (XmlNode no in nodeList) 
{    
   test.actual = no.Attributes["value"].Value;
   ...