Solved

How to modifying XML attributes using VBScript?

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

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

757 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