VB scrpit modify the configuration file (read and write XML)

Source: Internet
Author: User

'Vb scrpit modify the configuration file (XML ):

Dim wshshellset wshshell = Createobject ("wscript. shell ") set FSO = Createobject (" scripting. fileSystemObject ") 'create log file for the script set logstream = FSO. createtextfile ("updateconfig. log ", true) logstream. writeline "Start update config" 'assumes that the file to be modified is a database configuration file, to update datasource and datasourcemirror, the two elements datasource = "localhost" Export cemirror = "localhost" 'are assumed to be from another test. read the values of datasource and datasourcemirror in XML and update them to configfile. configtestpath = "test. XML "set xmldoc = Createobject (" msxml2.domdocument ") if (FSO. fileexists (testpath) Then MSG = nboxconfigfilepath & "exists" logstream. writeline MSG 'Load the test config file xmldoc. load (testpath) 'assumes test. the XML structure is relatively complex. Use XPath to search for the corresponding node: Set nodes cenode = xmldoc. selectsinglenode ("// test [@ name = 'datasource ']"). parentnode set parameter cemirrornode = xmldoc. selectsinglenode ("// test [@ name = 'your cemirror ']"). parentnode' gets the node attribute value datasource = pipeline cenode. getattribute ("name") Export cemirror = export cemirrornode. getattribute ("name") else MSG = "warning" & testpath & "doesn't exists" 'If the testpath does not exist, write the warning information to the log file logstream. writeline MSG end if set wshenv = wshshell. environment ("process") ', replace config Dir and configfile with the corresponding content filepath = "configdir/configfile. config "If (FSO. fileexists (filepath) Then MSG = filepath & "exists" 'If the filepath does not exist, write the warning information to the log file logstream. writeline MSG 'loading config file xmldoc. load (filepath) logstream. writeline "loading config file" & filepath 'get the datasource and mirror node set rt1_cenode = xmldoc. selectsinglenode ("// Add [@ key = 'datasource ']") set rt1_cemirrornode = xmldoc. selectsinglenode ("// Add [@ key = 'clock cemirror ']") 'set the value attribute for the datasource and mirror node rt1_cenode. setattribute "value", datasource rt1_cemirrornode. setattribute "value", export cemirror 'Save the config file xmldoc. save (filepath) logstream. writeline "Save the config file" logstream. writeline "finish the configuration" else MSG = "error:" & filepath & "doesn' t exists" end if logstream. close 'release wshshell set wshshell = nothing

 

Example of configfile. config:

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Add key = "datasource" value = "localhost"/>
<Add key = "datasourcemirror" value = "localhost"/>
<Add key = "initialcatalog" value = "testdb"/>
</Appsettings>
</Configuration>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.