VB scrpit 修改設定檔(讀寫XML)

來源:互聯網
上載者:User

‘VB scrpit 修改設定檔(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"  ' 假定要修改的為一個資料庫設定檔,要更新DataSource 和DataSourceMirror 兩個元素dataSource = "localhost"dataSourceMirror = "localhost" '假定要從另一個test.xml中讀取DataSource 和DataSourceMirror 的值,然後更新到 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)          ' 假定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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.