使用 vbscript 选择单节点



下面是我的xml文件的结构:

<configuration>
  <appSettings>
    <add key="ProductVersion" value="5.5.5"/>
    <add key="LogsDirectory" value="e:\Logs"/>
  </appSettings>
<configuration>

我正在尝试以下代码来获取日志目录的值:

configurationFilePath = "e:conf.xml"
Set xmlDoc = CreateObject("MSXML2.DomDocument.6.0")
xmlDoc.async = false
Call xmlDoc.load(configurationFilePath)
xpath1 = ".//configuration/appSettings/add[@key='LogsDirectory']/@value"
LogsDirectory = xmlDoc.selectSingleNode(xpath1)

但它给出错误作为所需的对象。

任何帮助都非常感谢。

谢谢

不使用 XML 错误检查框架的人是这样工作的:

Option Explicit
Dim oFS    : Set oFS   = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec    = oFS.GetAbsolutePathName(".19194544.xml")
Dim oXDoc  : Set oXDoc = CreateObject("MSXML2.DomDocument.6.0")
oXDoc.setProperty "SelectionLanguage", "XPath"
oXDoc.async = False
oXDoc.load sFSpec
If 0 = oXDoc.ParseError Then
   WScript.Echo sFSpec, "looks ok"
   Dim sXPath
   For Each sXpath In Array( _
       ".//configuration/appSettings/add[@key='LogsDirectory']/@value" _
   )
       Dim ndFnd : Set ndFnd = oXDoc.selectSingleNode(sXpath)
       If Not ndFnd Is Nothing Then
          WScript.Echo "found |" & ndFnd.xml & "|"
       Else
          WScript.Echo "not found |" & sXPath & "|"
       End If
   Next
Else
   WScript.Echo oXDoc.ParseError.Reason
End If

也可以在没有绳索的情况下进行蹦极。

在您的情况下,.ParseError.Reason

The following tags were not closed: configuration, configuration.

解释为什么没有要搜索的文档。至少这样可以避免在不使用 Set 的情况下将从 .selectSingleNode() 返回的节点分配给 LogsDirectory 时遇到的错误。

最新更新