(1) 使用带有基本错误检查的骨架,例如
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec = oFS.GetAbsolutePathName("...")
Dim oXML : Set oXML = CreateObject("Msxml2.DOMDocument")
oXML.setProperty "SelectionLanguage", "XPath"
' If namespace(s)
' oXML.setProperty "SelectionNamespaces", "...'"
oXML.async = False
oXML.load sFSpec
If 0 = oXML.parseError Then
WScript.Echo oXML.xml
WScript.Echo "-----------------"
Dim sXPath : sXPath = "..."
Dim ndFnd : Set ndFnd = oXML.selectSingleNode(sXPath)
If ndFnd Is Nothing Then
WScript.Echo sXPath, "not found"
Else
WScript.Echo ndFnd.nodeName, ndFnd.getAttribute("UserID")
WScript.Echo "-----------------"
ndFnd.setAttribute "UserID", "Changed"
WScript.Echo oXML.xml
oXML.save Replace(sFSpec, ".xml", "-o.xml")
End If
Else
WScript.Echo oXML.parseError.reason
End If
(2) 填写文件规范后,您意识到您的 XML 格式不正确。
script 13589885.vbs
nd tag 'gs:GlobalizationServices' does not match the start tag 'gs:GlobalizationService'.
(3) 由于 XML 包含 (a) 命名空间,您需要
oXML.setProperty "SelectionNamespaces", "xmlns:gs='urn:longhornGlobalizationUnattend'"
...
Dim sXPath : sXPath = "/gs:GlobalizationService/gs:UserList/gs:User"
(4) 现在它没有明显崩溃:
cscript 13589885.vbs
<gs:GlobalizationService xmlns:gs="urn:longhornGlobalizationUnattend">
<gs:UserList>
<gs:User UserID="Current"/>
</gs:UserList>
<gs:InputPreferences>
<gs:InputLanguageID Action="add" ID="0409:00000409" Default="true"/>
</gs:InputPreferences>
</gs:GlobalizationService>
-----------------
gs:User Current
-----------------
<gs:GlobalizationService xmlns:gs="urn:longhornGlobalizationUnattend">
<gs:UserList>
<gs:User UserID="Changed"/>
</gs:UserList>
<gs:InputPreferences>
<gs:InputLanguageID Action="add" ID="0409:00000409" Default="true"/>
</gs:InputPreferences>
</gs:GlobalizationService>
(5) 反思为什么xmldoc.Load("input.xml")
和xmlDoc.save("input.xml")
是坏的VBScript。