[学习日记]VB.net读取XML文件

一段用来读取XML文件信息的VB代码
使用了递归方式

{1F22F76B-16B7-4B5E-A2FB-85F741F589E2}0.jpg

VB.net代码如下:

Imports System.xml
Public Class Form1
    
Inherits System.Windows.Forms.Form

#Region 
" Windows 窗体设计器生成的代码 "

    Public Sub New()
        
MyBase.New()

        
'该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        
'在 InitializeComponent() 调用之后添加任何初始化

    
End Sub


    
'窗体重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        
If disposing Then
            
If Not (components Is NothingThen
                components.Dispose()
            
End If
        
End If
        
MyBase.Dispose(disposing)
    
End Sub


    
'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    
'注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents input As System.Windows.Forms.TextBox
    
Friend WithEvents outtext As System.Windows.Forms.TextBox
    
Friend WithEvents Button1 As System.Windows.Forms.Button
    
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.
input = New System.Windows.Forms.TextBox
        Me.outtext 
= New System.Windows.Forms.TextBox
        Me.Button1 
= New System.Windows.Forms.Button
        Me.SuspendLayout()
        
'
        'input
        '
        Me.input.Location = New System.Drawing.Point(168)
        Me.
input.Name = "input"
        Me.input.Size = New System.Drawing.Size(46421)
        Me.
input.TabIndex = 0
        Me.
input.Text = "http://127.0.0.1/fileup/people.xml"
        '
        'outtext
        '
        Me.outtext.BackColor = System.Drawing.SystemColors.HighlightText
        Me.outtext.BorderStyle 
= System.Windows.Forms.BorderStyle.FixedSingle
        Me.outtext.Location 
= New System.Drawing.Point(040)
        Me.outtext.Multiline 
= True
        Me.outtext.Name 
= "outtext"
        Me.outtext.ReadOnly = True
        Me.outtext.ScrollBars 
= System.Windows.Forms.ScrollBars.Both
        Me.outtext.Size 
= New System.Drawing.Size(624472)
        Me.outtext.TabIndex 
= 1
        Me.outtext.Text 
= "TextBox2"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(5048)
        Me.Button1.Name 
= "Button1"
        Me.Button1.Size = New System.Drawing.Size(9624)
        Me.Button1.TabIndex 
= 2
        Me.Button1.Text 
= "读  取"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(614)
        Me.ClientSize 
= New System.Drawing.Size(632517)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.outtext)
        Me.Controls.Add(Me.
input)
        Me.Name 
= "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    
End Sub


#
End Region

    
Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click
        
Dim doc As xmldocument = New xmldocument
        
Dim y As String
        doc.Load(
input.Text)
        
Dim rootnode As XmlElement = doc.DocumentElement
        outtext.Text 
= ""
        enumeratenode(rootnode, 0)
    
End Sub


    
Private Sub enumeratenode(ByVal node As XmlNode, ByVal indentval As Integer)
        
Dim type As String
        
Select Case node.NodeType
            
Case XmlNodeType.Element
                type 
= "元素"
            Case XmlNodeType.Text
                type 
= "文本"
            Case XmlNodeType.Comment
                type 
= "注释"
            Case Else
                outtext.AppendText(
".")
        
End Select

        outtext.AppendText(type 
& "节点找到")

        
Select Case node.NodeType
            
Case XmlNodeType.Element
                outtext.AppendText(
",name=" & node.Name & vbCrLf)
            
Case XmlNodeType.Text
                outtext.AppendText(
",content=" & node.Value & vbCrLf)
            
Case XmlNodeType.Comment
                outtext.AppendText(
",content=" & node.Value & vbCrLf)
            
Case Else
                outtext.AppendText(
".")
        
End Select

        
If Not node.Attributes Is Nothing Then
            
If node.Attributes.Count <> 0 Then
                outtext.AppendText(
"此节点有属性:")
                
Dim attr As XmlAttribute
                
For Each attr In node.Attributes
                    outtext.AppendText(attr.Name 
& " =" & attr.Value & vbCrLf)
                
Next
            
End If
        
End If

        
If node.HasChildNodes Then
            outtext.AppendText(
"此节点有子节点:" & vbCrLf)
            
Dim child As XmlNode
            
For Each child In node.ChildNodes
                enumeratenode(child, indentval 
+ 1)
            
Next
        
End If
    
End Sub

End Class
posted @ 2005-02-28 13:45  Aowind  阅读(12047)  评论(0编辑  收藏  举报