使用VB6编辑XML的属性值

时间:2012-05-08 12:26:43

标签: xml vb6

我需要使用VB6更新bkp_path的属性值。

XML文件

<ServerDetails>
    <Param src_path = "D:\Shapes\Rectangle" bkp_path = "D:\Colors\Red"/>
</ServerDetails>

我可以使用

从XML文件中读取值

VB6代码

Dim doc As New MSXML2.DOMDocument
Set doc = New MSXML2.DOMDocument
Dim success As Boolean
'Load Config.xml
success = doc.Load("\Config\config.xml")

If success = False Then
  MsgBox ("Unable to locate the configuration file")
  Exit Function
Else
  Dim nodeList As MSXML2.IXMLDOMNodeList

  Set nodeList = doc.selectNodes("/ServerDetails/Param")

  If Not nodeList Is Nothing Then
     Dim node As MSXML2.IXMLDOMNode

     For Each node In nodeList
        srcpath = node.selectSingleNode("@src_path").Text
        bkpPath = node.selectSingleNode("@bkp_path").Text            
     Next node
  End If
End If

但无法弄清楚如何更新属性值。

2 个答案:

答案 0 :(得分:3)

您需要获取对节点对象的引用,然后调用setAttribute()来指定新值:

node.setAttribute "bkp_path", "wibble"

您的代码还会读取所有Param个节点的值,但您可能只想使用第一个节点或更新特定节点。

答案 1 :(得分:2)

这就是诀窍:

node.selectSingleNode("@bkp_path").Text = "D:\Colors\Blue"