VB6.0操作XML文件

XML文件如下:




1
自由飞翔




2
自由






以下是VB6.0的代码:

'工程->引用->Mircosoft XML6.0

Dim XMLDoc As DOMDocument
Dim root As IXMLDOMNode

Dim xlst As IXMLDOMNodeList, xlst1 As IXMLDOMNodeList
Dim xn As IXMLDOMNode
Dim xnf As IXMLDOMNode
Dim xe As IXMLDOMElement

Set XMLDoc = New DOMDocument

XMLDoc.Load "F:\example\test.xml" 'xml文件路径根据存放路径不同而不同!

Set xn = XMLDoc.selectSingleNode("testlist")

'以下为读取xml文件内容,这里用ListView控件显示
Set xlst = xn.childNodes
ListView1.ListItems.Clear
For Each xnf In xlst
Set xe = xnf
Set xlst1 = xe.childNodes
With ListView1.ListItems.Add(, , xlst1.Item(0).Text)
.SubItems(1) = xlst1.Item(1).Text
.SubItems(2) = xlst1.Item(2).Text
End With
Next



'以下是插入一个节点(这里插入一个id=3,name=飞翔,time=date 的节点)

Dim XMLId As IXMLDOMElement
Dim XMLName As IXMLDOMElement
Dim XMLTime As IXMLDOMElement

Dim returnValue As IXMLDOMNode

Set xe = XMLDoc.createElement("test") '创建一个节点
Set XMLId = XMLDoc.createElement("id")

XMLId.Text = 3

Set returnValue = xe.appendChild(XMLId)

Set XMLName = XMLDoc.createElement("name")

XMLName.Text = "飞翔"

Set returnValue = xe.appendChild(XMLName)

Set XMLTime = XMLDoc.createElement("time")

XMLTime.Text = Date

Set returnValue = xe.appendChild(XMLTime)

Set returnValue = xn.appendChild(xe) '添加到节点中

XMLDoc.Save "F:\example\test.xml"



'删除XML中id=2的节点

Set xlst = XMLDoc.SelectSingleNode("testlist").ChildNodes

For Each xnf In xlst
Set xe = xnf
If xe.childNodes(0).Text = "2" Then
xn.removeChild xe
End If

Next xnf

XMLDoc.Save "F:\example\test.xml"

你可能感兴趣的:(VB&BBS,XML,F#)