Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to modifying XML attributes using VBScript?

Posted on 2006-11-13
4
Medium Priority
?
6,213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 4

Accepted Solution

by:
hclgroup earned 252 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 248 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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

688 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