首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

VB.net 读取XML 生成 Treeview 有关问题,100分,给分

2012-01-31 
VB.net 读取XML 生成Treeview 问题,100分,在线等给分?xmlversion 1.0 encoding gb2312 ?treenode

VB.net 读取XML 生成 Treeview 问题,100分,在线等给分
<?xml   version= "1.0 "   encoding= "gb2312 "?>
<treenodes>
<treenode   text= "aa ">
    <treenode   text= "a1 "   />
</treenode>      
<treenode   text= "bb ">
    <treenode   text= "b1 "   />  
    <treenode   text= "b2 "   />  
</treenode>
</treenodes>

希望生成  
aa
    a1
bb
    b1
    b2
的树形结构  
在线等,解决给分

[解决办法]
Dim document As New System.Xml.XmlDataDocument() document.Load( "../contacts.xml ") populateTreeControl(document.DocumentElement, _ TreeView1.Nodes)

Private Sub populateTreeControl( _ ByVal document As System.Xml.XmlNode, _ ByVal nodes As _ System.Windows.Forms.TreeNodeCollection)
Dim node As System.Xml.XmlNode
For Each node In document.ChildNodes
' If the element has a value, display it;
' otherwise display the first attribute
' (if there is one) or the element name
' (if there isn 't)
Dim [text] As String
If node.Value <> Nothing Then
[text] = node.Value
Else
If Not node.Attributes Is Nothing And node.Attributes.Count > 0 Then
[text] = node.Attributes(0).Value
Else
[text] = node.Name
End If
End If
Dim new_child As New TreeNode([text]) nodes.Add(new_child) populateTreeControl(node, new_child.Nodes)
Next node
End Sub
[解决办法]
'注释: Load saved values from XML.
Private Sub LoadValues(FilePath)
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode

'注释: Load the document.
Set xml_document = New DOMDocument
'xml_document.Load m_AppPath & "Values.xml "
xml_document.Load FilePath

'注释: If the file doesn注释:t exist, then
'注释: xml_document.documentElement is Nothing.
If xml_document.documentElement Is Nothing Then
'注释: The file doesn注释:t exist. Do nothing.
Exit Sub
End If

'注释: Find the Values section.
Set values_node = xml_document.selectSingleNode( "police ")

'注释: Read the saved values.
txtFirstName.Text = GetNodeValue(values_node, "groupid ", "??? ")
txtLastName.Text = GetNodeValue(values_node, "sn ", "??? ")
txtStreet.Text = GetNodeValue(values_node, "name ", "??? ")
txtCity.Text = GetNodeValue(values_node, "reason ", "??? ")
txtState.Text = GetNodeValue(values_node, "comment ", "没有 ")
'txtZip.Text = GetNodeValue(values_node, "reason ", "??? ")
End Sub

Private Sub populateTreeControl( _ ByVal document As System.Xml.XmlNode, _ ByVal nodes As _ System.Windows.Forms.TreeNodeCollection)
Dim node As System.Xml.XmlNode
For Each node In document.ChildNodes
' If the element has a value, display it;
' otherwise display the first attribute
' (if there is one) or the element name
' (if there isn 't)


Dim [text] As String
If node.Value <> Nothing Then
[text] = node.Value
Else
If Not node.Attributes Is Nothing And node.Attributes.Count > 0 Then
[text] = node.Attributes(0).Value
Else
[text] = node.Name
End If
End If
Dim new_child As New TreeNode([text]) nodes.Add(new_child) populateTreeControl(node, new_child.Nodes)
Next node
End Sub

热点排行