Solved

How to modifying XML attributes using VBScript?

Posted on 2006-11-13
4
5,893 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
The viewer will learn how to dynamically set the form action using jQuery.

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now