Noemie
Noemie

Reputation: 141

VBScript modify a value in a XML file

I'm learning VBScript and I am trying to modify values on a xml file using vbscript but I always get an error with the variable "colNode":

Microsoft VBScript Runtime Error: This object does not support this property or method: 'colNode.Text

Here is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
    <Text Name="Text1">Lorem ipsum dolor sit amet, consectetur adipiscing elit. </Text>
    <Text Name="Text2">Aliquam mattis quam lorem, ut sollicitudin dolor dignissim sed. </Text>
    <Text Name="Text3">Fusce cursus tellus eu consectetur rutrum.</Text>
</Configuration>

And here is my VBScript code:

    Set xmlDoc = _
    CreateObject("Microsoft.XMLDOM")

    xmlDoc.Async = "False"
    xmlDoc.Load(".\message.xml")

    
    Set colNode=xmlDoc.selectNodes _
  ("//Configuration/Text[@Name='Text2']")

    
    colNode.Text = "It's a test!"  
    xmlDoc.Save "C:\Scripts\Audits.xml"

Upvotes: 1

Views: 562

Answers (1)

Theo
Theo

Reputation: 61068

The SelectNodes method returns a collection of nodes (in this case a collection of just one) and if you want to use that, you need to iterate through that collection in order to change the text in every node in the collection:

Set colNode=xmlDoc.SelectNodes("//Configuration/Text[@Name='Text2']")
For Each node In colNode
    node.Text = "It's a test!"
Next

In your example however, you are seeking to change the text in only one specific node, so for that you ca use the SelectSingleNode method:

Set colNode=xmlDoc.SelectSingleNode("//Configuration/Text[@Name='Text2']")
colNode.Text = "It's a test!"

Upvotes: 2

Related Questions