我做了个读取xml文件的vb程序,遇到了一些问题,当遇见如下格式时:只读取第一个元素45,读不到第2,第3个请高手帮助
如:
<cwj>
<ck>45<ck>
<ck>237<ck>
<ck>898<ck>
</cwj>
我的vb程序如下,引用了xmldom:
Option Explicit
Private p_AppPath As String
Private Sub Form_Load()
' 获得程序运行目录
p_AppPath = App.Path
If Right$(p_AppPath, 1) <> "\" Then p_AppPath = p_AppPath & "\"
' 加载值
LoadValues
End Sub
Private Sub Form_Unload(Cancel As Integer)
' 保存现有的值
SaveValues
End Sub
Private Sub LoadValues()
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode
' 载入文件
Set xml_document = New DOMDocument
xml_document.Load p_AppPath & "Online00.xml"
If xml_document.documentElement Is Nothing Then
Exit Sub
End If
'寻找节点
Set values_node = xml_document.selectSingleNode("OnlineData")
' 读取各个节点的值
txtName.Text = GetNodeValue(values_node, "Date", "???")
txtSex.Text = GetNodeValue(values_node, "Time", "???")
txtBirthday.Text = GetNodeValue(values_node, "Name", "???")
txtAddress.Text = GetNodeValue(values_node, "PositionX", "???")
txtZip.Text = GetNodeValue(values_node, "Value", "???")
End Sub
' 返回各个节点的值
Private Function GetNodeValue(ByVal start_at_node As IXMLDOMNode, ByVal node_name As String, _
Optional ByVal default_value As String = "") As String
Dim value_node As IXMLDOMNode
Set value_node = start_at_node.selectSingleNode(".//" & node_name)
If value_node Is Nothing Then
GetNodeValue = default_value
Else
GetNodeValue = value_node.Text
End If
End Function
' 保存现有的值
Private Sub SaveValues()
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode
' 建立XML文件
Set xml_document = New DOMDocument
Set values_node = xml_document.createElement("OnlineData")
xml_document.appendChild values_node
CreateNode values_node, "Date", txtName.Text
CreateNode values_node, "Time", txtSex.Text
CreateNode values_node, "Name", txtBirthday.Text
CreateNode values_node, "PositionX", txtAddress.Text
CreateNode values_node, "Value", txtZip.Text
' 保存XML文件
xml_document.save p_AppPath & "Online00.xml"
End Sub
Private Sub CreateNode(ByVal parent As IXMLDOMNode, _
ByVal node_name As String, ByVal node_value As String)
Dim new_node As IXMLDOMNode
Set new_node = parent.ownerDocument.createElement(node_name)
new_node.Text = node_value
parent.appendChild new_node
End Sub