0

I have looked through several threads here on how to locate an XML node and change an attribute. However, I cannot find a solution to my problem:

I have an XML node which has several nodes under it with the same name (but different attributes). For example:

<Configuration>
  <ConfigOptions>
    <add key="Localize" value="Off" />
    <add key="Cache" value="Database" />
    <add key= etc........

I need to use a simple VBScript to locate the <add> node where key="Cache" and then change the value to something else.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user3791158
  • 11
  • 1
  • 1
  • 1
    First, understand exactly what you are trying to find. I suggest reviewing A beginner's guide to the XML DOM: http://msdn.microsoft.com/en-us/library/aa468547.aspx. The key in key="Cache" is an attribute. Also, look here: http://stackoverflow.com/questions/2225958/using-asp-vb-to-get-value-of-a-node-attribute. Then, add the code you've tried and where you're getting stuck. – SeraM Jun 30 '14 at 18:02
  • 1
    See if http://stackoverflow.com/a/11782233/603855 helps. – Ekkehard.Horner Jun 30 '14 at 18:13

1 Answers1

4

use xpath syntax to select the node

Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0")
xmlDoc.load strXMLPath
Set node = xmlDoc.selectNodes("//Configuration/ConfigOptions/add[@key='Cache']")
strOldValue = node.item(0).attributes.getNamedItem("value").text

then change the value

node.item(0).attributes.getNamedItem("value").text = strNewValue

then save the xml file

xmlDoc.save strXMLPath
langstrom
  • 1,652
  • 11
  • 14