use MSXML2.DOMDocument with XML

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Been thanked: 2 times
Contact:

use MSXML2.DOMDocument with XML

Post by MigrationUser »

22 Mar 2013 20:51
22152Bill


I didn't notice any information on XML files with VBScript

So I thought I would donate some simple commands in hopes that someone would add to them.


Modify XML: (web.config for webservices)
--------------------------------------------------------------------

Code: Select all

Set xmlDoc = CreateObject("MSXML2.DOMDocument")

xmlDoc.async = False  
xmlDoc.load("web.config")

Set objRoot = xmlDoc.documentElement

Set objConfigProtectedData = xmlDoc.createElement("configProtectedData")

Set objProvidersNode = xmlDoc.createElement("providers")
Set objProviderClearNode = xmlDoc.createElement("clear")
Set objProviderAddNode = xmlDoc.createElement("add")

objProviderAddNode.SetAttribute "name", "myEncryptionProvider"
objProviderAddNode.SetAttribute "type", "System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL"
objProviderAddNode.SetAttribute "useMachineContainer", "true"
objProviderAddNode.SetAttribute "keyContainerName", "NETWORK_SERVICE"

objProvidersNode.appendChild objProviderClearNode
objProvidersNode.appendChild objProviderAddNode
objConfigProtectedData.appendChild objProvidersNode

objRoot.insertBefore objConfigProtectedData, objRoot.FirstChild
objRoot.Normalize 

xmlDoc.Save "web.config"
--------------------------------------------------------------------------------------------------------------------------

Read XML:
--------------------------------------------------------------------------------------------------------------------------

Code: Select all

Set xmlDoc = CreateObject("MSXML2.DOMDocument")

xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.load(pthome & "web.config")

strName = cpdNode.getAttribute("name")
strType  = cpdNode.getAttribute("type")
strWMC  = cpdNode.getAttribute("useMachineContainer")
strkey    = cpdNode.getAttribute("keyContainerName")
strText   = cpdNode.text
------------------------------------------------------------------------------------------------------------------------------

Remove XML node:
------------------------------------------------------------------------------------------------------------------------------

Code: Select all

Dim objXML, objNode

Set objXML = CreateObject("MSXML2.DOMDocument")
objXML.async = false
objXML.Load("web.config")

Set objNode = objXML.documentElement.selectSingleNode("//configuration/configProtectedData")
objNode.parentNode.removeChild(objNode)

objXML.Save ("web.config")
--------------------------------------------------------------------------------------------------------------------------------

original thread: https://ss64.org/oldforum/viewtopic.php?id=1677
Post Reply