Solved

How to modifying XML attributes using VBScript?

Posted on 2006-11-13
4
5,977 Views
Last Modified: 2013-11-19
I have a scenario where I need to parse through and XML file and change one of the attributes of one of the elements.

set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = "false"
xmlDoc.load("c:\temp\machine.xml")

for each node in xmlDoc.documentElement.childNodes
    if node.nodename = "connectionStrings" then
        Wscript.Echo(node.nodename)
        for each childNode in node.childnodes
            Wscript.Echo(childNode.nodename)
            for each att in childNode.attributes
                Wscript.Echo(att.name)
                Wscript.Echo(att.text)                
            next            
        next    
    end if
next

I need to change the value of the data source attribute for the connection string.  The xml extract is below.  How do I replace the attribute?

<connectionStrings>
    <add name="Test" providerName="System.Data.SqlClient" connectionString="data source=test;initial catalog=TPCS;trusted_connection=true; persist security info=True; packet size=4096"/>
  </connectionStrings>

Many thanks.
0
Comment
Question by:markerasmus
4 Comments
 
LVL 4

Accepted Solution

by:
hclgroup earned 63 total points
ID: 17932453
You can use setAttribute to set the value of the named attribute
http://www.rasim.dk/filer/tutorials/XMLDOC/element_setAttribute.html

and then save the document
Call xmlDoc.save("c:\temp\machine.xml")
0
 
LVL 5

Assisted Solution

by:sivachirravuri
sivachirravuri earned 62 total points
ID: 17936129
Hi,
Check following sample code for modyfying XML.

function doUpdate()
      
            dim objNode, objDOM, strXPath
            
            'Create an XML DOM object from the XML file
            Set objDOM  = CreateObject("Microsoft.XMLDOM")            
            objDOM.async = false
            objDOM.load("Customers.xml")
                  
                  
            'Display the original XML values
            txtOrig.value = objDOM.xml
            

            'Below are some examples of how to update the contents of an XML Element            
            strXPath = "/customers/customer[@CustomerID='SIVA']/@CompanyName"
            objDOM.selectSingleNode(strXPath).text = "new value 1"

            objDOM.selectSingleNode( "/customers/customer[@CustomerID='SIVA']/@CompanyName").text = "new value 2"
            objDOM.selectSingleNode( "/customers/customer[0]/@CompanyName").text = "new value 3"
            
                        txtNew.value = objDOM.xml
            
      end function
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create self-signed certificate on 2012 r2 Server Core? 5 31
Recover options for a failed domain. 4 54
How to use Powershell data from SQL 151 64
Export AD group members. 1 28
This article explains the steps required to use the default Photos screensaver to display branding/corporate images
This article shows theĀ method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your propā€¦

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question