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

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

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

VB.net代码如下:

None.gif Imports  System.xml
ExpandedBlockStart.gifContractedBlock.gif
Public   Class Form1 Class Form1
InBlock.gif    
Inherits System.Windows.Forms.Form
InBlock.gif
InBlock.gif#Region 
" Windows 窗体设计器生成的代码 "
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Sub New()Sub New()
InBlock.gif        
MyBase.New()
InBlock.gif
InBlock.gif        
'该调用是 Windows 窗体设计器所必需的。
InBlock.gif
        InitializeComponent()
InBlock.gif
InBlock.gif        
'在 InitializeComponent() 调用之后添加任何初始化
InBlock.gif

ExpandedSubBlockEnd.gif    
End Sub

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

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

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

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub enumeratenode()Sub enumeratenode(ByVal node As XmlNode, ByVal indentval As Integer)
InBlock.gif        
Dim type As String
InBlock.gif        
Select Case node.NodeType
InBlock.gif            
Case XmlNodeType.Element
InBlock.gif                type 
= "元素"
InBlock.gif
            Case XmlNodeType.Text
InBlock.gif                type 
= "文本"
InBlock.gif
            Case XmlNodeType.Comment
InBlock.gif                type 
= "注释"
InBlock.gif
            Case Else
InBlock.gif                outtext.AppendText(
".")
InBlock.gif        
End Select
InBlock.gif
InBlock.gif        outtext.AppendText(type 
& "节点找到")
InBlock.gif
InBlock.gif        
Select Case node.NodeType
InBlock.gif            
Case XmlNodeType.Element
InBlock.gif                outtext.AppendText(
",name=" & node.Name & vbCrLf)
InBlock.gif            
Case XmlNodeType.Text
InBlock.gif                outtext.AppendText(
",content=" & node.Value & vbCrLf)
InBlock.gif            
Case XmlNodeType.Comment
InBlock.gif                outtext.AppendText(
",content=" & node.Value & vbCrLf)
InBlock.gif            
Case Else
InBlock.gif                outtext.AppendText(
".")
InBlock.gif        
End Select
InBlock.gif
InBlock.gif        
If Not node.Attributes Is Nothing Then
InBlock.gif            
If node.Attributes.Count <> 0 Then
InBlock.gif                outtext.AppendText(
"此节点有属性:")
InBlock.gif                
Dim attr As XmlAttribute
InBlock.gif                
For Each attr In node.Attributes
InBlock.gif                    outtext.AppendText(attr.Name 
& " =" & attr.Value & vbCrLf)
InBlock.gif                
Next
InBlock.gif            
End If
InBlock.gif        
End If
InBlock.gif
InBlock.gif        
If node.HasChildNodes Then
InBlock.gif            outtext.AppendText(
"此节点有子节点:" & vbCrLf)
InBlock.gif            
Dim child As XmlNode
InBlock.gif            
For Each child In node.ChildNodes
InBlock.gif                enumeratenode(child, indentval 
+ 1)
InBlock.gif            
Next
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Sub

ExpandedBlockEnd.gif
End Class
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值