VB scrpit 修改配置文件(读写XML)

‘VB scrpit 修改配置文件(XML): 

Dim WshShell
Set 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"  

' 假定要修改的为一个数据库配置文件,要更新DataSource 和DataSourceMirror 两个元素
dataSource = "localhost"
dataSourceMirror = "localhost" 

'假定要从另一个test.xml中读取DataSource 和DataSourceMirror 的值,然后更新到 configFile.config
testPath= "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)
     
     ' 假定test.xml的结构相对复杂,用xpath搜索出对应的节点:    
     Set dataSourceNode = xmlDoc.selectSingleNode("//test[@name='DataSource']").parentNode
     Set dataSourceMirrorNode = xmlDoc.selectSingleNode("//test[@name='DataSourceMirror ']").parentNode
     
     ' 取得节点的属性值
     dataSource = dataSourceNode.getAttribute("name")
     dataSourceMirror = dataSourceMirrorNode.getAttribute("name")
  Else
    msg = "Warning" & testPath & " doesn't exists "   

    ' 如果testPath 路径不存在那么将警告信息写入log文件 
    logStream.writeline  msg   
  End If 
  
Set WshEnv = WshShell.Environment("PROCESS")

' 请注意将config dir 和configFile 替换成对应的内容 
filePath = “configdir/configFile.config" 

If (fso.FileExists(filePath)) Then
     msg = filePath & " exists"    

      ' 如果filePath 路径不存在那么将警告信息写入log文件 
     logStream.writeline  msg
     
     'Loading config file 
     xmlDoc.load(filePath) 
     logStream.writeline "Loading config file " & filePath
     
     'Get the datasource and mirror node
     Set rtDataSourceNode = xmlDoc.selectSingleNode("//add[@key='DataSource']") 
     Set rtDataSourceMirrorNode = xmlDoc.selectSingleNode("//add[@key='DataSourceMirror']")
     
     'Set the value attribute for the datasource and mirror node
     rtDataSourceNode.setAttribute "value", dataSource 
     rtDataSourceMirrorNode.setAttribute "value", dataSourceMirror     
     
     '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

‘ 释放WshShell 
 Set WshShell = Nothing

 

configFile.config示例

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="DataSource" value="localhost" />
    <add key="DataSourceMirror" value="localhost" />
    <add key="InitialCatalog" value="TestDB" />
  </appSettings>
</configuration>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值