jmjaeckels
jmjaeckels

Reputation: 3

VBA Loop Through XML Nodes Using SelectSingleNode

I am trying to update node values in a .xml file using a loop.

The oXMlFile.SelectSingleNode line shown correctly updates a single node, but I don't know how to modify the code so that I can loop though all the nodes and update the values.

Any help would be appreciated.

Set oXMLFile = CreateObject("Microsoft.XMLDOM")
oXMLFile.Load (ConfigFile)

'Update Node Attributes

Dim ii, TotChan As String
 ii = 0
 TotCh = 500

 Do While (ii < TotCh - 1)


Set CalibrationDateTimeNode=oXMLFile.SelectSingleNode("/HConfig/Signal[0]/CalibrationDateTime")

CalibrationDateTimeNode.Text = "2016-04-16"

 ii = ii + 1
 Loop

Upvotes: 0

Views: 1243

Answers (1)

vacip
vacip

Reputation: 5426

I'm not quite sure what you exactly need, but to loop through xml nodes, use this:

Dim calibrationDateTimeNode As IXMLDOMNode
Dim colNodes As IXMLDOMNodeList

Set colNodes = oXMLFile.selectNodes("/HConfig/Signal[0]/...WhateverYouNeed")
For Each CalibrationDateTimeNode In colNodes
   CalibrationDateTimeNode.Text = "2016-04-16"
Next

The SelectNodes command can selet a node list, and then you can iterate its elements. Using a for each makes it a lot easier than a do-while. Not to mention faster, if I'm not mistaken.

Upvotes: 1

Related Questions