标题:[求助]用vb读取xml遇到了困难
只看楼主
cwjmx
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-2-23
 问题点数:0 回复次数:2 
[求助]用vb读取xml遇到了困难

我做了个读取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

搜索更多相关主题的帖子: xml 困难 
2006-03-02 14:33
VB爱上我
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:478
专家分:52
注 册:2005-10-14
得分:0 
4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444

IT618资讯网 提供服务端开发,前端开发,网页特效,热门开源系统研究,软件下载,站长建站,淘宝开店等学习资料.
2006-03-02 16:31
糊涂虫
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2006-2-11
得分:0 
FSO

Wǒ會習慣①個人荇走,亨受着自由,雖然偶倁噵,自由的代价是寂寞!
2006-03-02 17:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-47995-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.636359 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved