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

前端之家收集整理的这篇文章主要介绍了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>

原文链接:https://www.f2er.com/vb/261436.html

猜你在找的VB相关文章