Solved

How to modifying XML attributes using VBScript?

Posted on 2006-11-13
4
6,052 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 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article explains the steps required to use the default Photos screensaver to display branding/corporate images
Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
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…

729 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