Xml node for each loop

I am trying to skip an Xml file and display the value for the account in the 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 re-recording the first record, how can I go to the next record?

Thanks for your input in advance.

+6
c # xml loops foreach nodes
source share
7 answers

You re-assign node to the same element from testDoc . Unclear what is test.account (maybe an error for test.actual )?

no is a variable that will nodeList over the contents of nodeList - I assume that you intended to use this.

EDIT after editing OP Now you have shown us what nodeList is, I suspect you want to do something like this:

 XmlNodeList nodeList = testDoc.SelectNodes("/details/row/var[@name='account']"); foreach (XmlNode no in nodeList) { test.actual = no.Attributes["value"].Value; ... 
+13
source share
  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); } } 
+5
source share

Here is an example of the parent value of node to get information about child nodes. I use ReportItems child nodes ParentNode and Print only.

  xmldoc.Load(rdlFile); StringBuilder sb=new StringBuilder(); XmlNode node = xmldoc.GetElementsByTagName("ReportItems")[0]; XmlNodeList list = node.ChildNodes; atributes=new string[node.ChildNodes.Count]; int l = 0; for (int j = 0; j < node.ChildNodes.Count; j++) { if (list[j].Name == "Image") { XmlAttributeCollection att = list[j].Attributes; atributes[l] = att[0].Value.ToUpper(); } l++; } for (int i = 0; i < node.ChildNodes.Count; i++) { if (searchText.Text.ToUpper() == atributes[i]) { XmlNodeList lastlist = node.ChildNodes; XmlNodeList endlist = lastlist[i].ChildNodes; for (int k = 0; k < endlist.Count; k++) { sb.Append(endlist[k].Name+" - "+ endlist[k].InnerText); sb.Append("\n"+"\n"); } } } 

let me know if you have any doubts.

+3
source share

Try it,

 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); } 
+2
source share

Try the following:

  //Create an xml reader; XmlDocument _xmlDocument = new XmlDocument(); _xmlDocument.Load(/*File Name here*/); //Select the element with in the xml you wish to extract; XmlNodeList _nodeList = _xmlDocument.SelectNodes("/details/row/var[@name='account']"); //Display the values in the node list to the screen; foreach (XmlNode _node in _nodeList) { String _nodeValue = _node.InnerText.ToString(); MessageBox.Show(_nodeValue.ToString()); } 
+1
source share

I'm not 100% sure, but you might need recursion. If not, it should look like this:

 XmlDocument doc = //etc.. foreach(XmlNode node in doc.ChildNodes) { if(node.Name == "account") { MessageBox.Show(node.Value); } } 
0
source share

You should not waste time reading xml node on node. Try Deserialization :

0
source share

All Articles