Looping through XmlNodeList
-
Hi, I have the following code, which picks up 43 different nodes from my XML document XmlNodeList amortNodes = amortDoc.SelectNodes("// TValueAmortizationSchedule/AmortizationLine"); each node looks like this: 8 09/01/2006 100000000 0 0 0 0 100000000 100000000 13 Now I am trying to loop through the node list to get get the nodes with AmortizationLineType = 8 foreach (XmlNode amortNode in amortNodes) { amortType = amortNode.SelectSingleNode("//AmortizationLine/ AmortizationLineType").InnerText; if (amortType.Equals("8")) { count++; ........... } } Now I know from my debugging, that not all 43 nodes fulfil this criteria (ie amortType=8). However, when I run this application, the amortType always comes back as 8 (which is the value on only the first node), which seems to tell me it is not picking up the values after that? Any ideas? Thanks for your help.
-
Hi, I have the following code, which picks up 43 different nodes from my XML document XmlNodeList amortNodes = amortDoc.SelectNodes("// TValueAmortizationSchedule/AmortizationLine"); each node looks like this: 8 09/01/2006 100000000 0 0 0 0 100000000 100000000 13 Now I am trying to loop through the node list to get get the nodes with AmortizationLineType = 8 foreach (XmlNode amortNode in amortNodes) { amortType = amortNode.SelectSingleNode("//AmortizationLine/ AmortizationLineType").InnerText; if (amortType.Equals("8")) { count++; ........... } } Now I know from my debugging, that not all 43 nodes fulfil this criteria (ie amortType=8). However, when I run this application, the amortType always comes back as 8 (which is the value on only the first node), which seems to tell me it is not picking up the values after that? Any ideas? Thanks for your help.
The problem is the XPath expression passed to the
SelectSingleNode
method. The double slash at the beginning causes the selection of all nodes in the document from the current node that match the selection no matter where they are, so you're always selecting the first of the AmortizationLineType elements of all AmortizationLine elements. Try using the following XPath expressionamortNode.SelectSingleNode("./AmortizationLineType")
instead.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook