Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml node reading for each loop

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.

like image 652
Ebikeneser Avatar asked Jan 10 '11 18:01

Ebikeneser


3 Answers

Try this,

XmlDocument xdoc = new XDocument();

xdoc.Load("*/File/*"); 
string xmlcontents = xdoc.InnerXml;

var xpath = "(/details/row/var[@name='account'])";

XmlNodeList lists = xdoc.DocumentElement.SelectNodes(xpath);

foreach (XmlNode _node in lists)
{
    string _nodeValue = _node.InnerText;
    MessageBox.Show(_nodeValue);
}
like image 86
DotNetSpartan Avatar answered Sep 21 '22 10:09

DotNetSpartan


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;
   ...
like image 15
Chris Dickson Avatar answered Oct 07 '22 10:10

Chris Dickson


        XmlDocument doc = new XmlDocument();
        doc.Load("d:\\test.xml");
        XmlNodeList node = doc.GetElementsByTagName("w:r");
        foreach (XmlNode xn in node)
        {
            try
            {
                if (xn["w:t"].InnerText != null)
                {
                    if (xn["w:t"].InnerText == "#")
                    {
                        string placeHolder = xn["w:t"].InnerText;
                        foreach (XmlNode a in node)
                        { 
                            if (a["w:t"].InnerText != "#")
                            {
                                string placeHolder1 = a["w:t"].InnerText;
                            }
                        } 
                    }
                }
            }

            catch (Exception e)
            {
                Console.Write(e);
            }
        } 
like image 5
Ram Avatar answered Oct 07 '22 11:10

Ram