VB.NET读取XML文件实现技巧分享

开发 后端
VB.NET读取XML文件是开发人员经常会使用到的一个编程操作。初学者们在学习的过程中需要对这样的基础方法进行详细的解读,才能方便以后的应用。

VB.NET未开发人员带来了不一样的开发方式。其中特有的各种特点和语言特性为编程人员开发程序提供了很大的帮助。我们今天就来看一段实现VB.NET读取XML文件的VB代码。使用了递归方式。#t#

 

VB.NET读取XML文件代码如下:

  1. Imports System.xml  
  2. Public Class Form1Class Form1  
  3. Inherits System.Windows.Forms.Form  
  4. #Region " Windows 窗体设计器生成的代码 "  
  5. Public Sub New()Sub New()  
  6. MyBase.New()  
  7. '该调用是 Windows 窗体设计器所必需的。  
  8. InitializeComponent()  
  9. '在 InitializeComponent() 
    调用之后添加任何初始化  
  10. End Sub 

 

 

'窗体重写 dispose 以清理组件列表。  
Protected Overloads Overrides 
Sub Dispose()Sub Dispose(ByVal 
disposing As Boolean)  
If disposing Then   If Not (components Is Nothing) 
Then  
components.Dispose()   End If   End If   MyBase.Dispose(disposing)   End Sub 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

 

 

'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()
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(16, 8)  
Me.input.Name = "input"  Me.input.Size = New System.
Drawing.Size(464, 21)  
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(0, 40)  
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(624, 472)  
Me.outtext.TabIndex = 1  Me.outtext.Text = "TextBox2"  '   'Button1   '   Me.Button1.Location = New 
System.Drawing.Point(504, 8)  
Me.Button1.Name = "Button1"  Me.Button1.Size = New System.
Drawing.Size(96, 24)  
Me.Button1.TabIndex = 2  Me.Button1.Text = "读 取"  '   'Form1   '   Me.AutoScaleBaseSize = New 
System.Drawing.Size(6, 14)  
Me.ClientSize = New System.
Drawing.Size(632, 517)  
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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.

 

 

#End Region  
Private Sub Button1_Click()
Sub Button1_Click(ByVal sender 
As System.Object, ByVal 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 Su 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

 

 

Private Sub enumeratenode()
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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

 

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

 

 

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

 

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

VB.NET读取XML文件实现代码的编写方法如上所示。

责任编辑:曹凯 来源: 博客园
相关推荐

2010-01-18 16:33:57

VB.NET加密文件

2010-01-18 18:50:26

VB.NET鼠标手势

2010-01-15 19:04:09

2010-01-14 16:04:32

VB.NET显示时间

2010-01-18 16:41:47

VB.NET用户登录页

2010-01-18 10:26:19

VB.NET中心旋转图

2010-01-13 10:25:30

VB.NET文件夹操作

2010-01-22 11:02:30

VB.NET创建新变量

2010-01-13 15:52:59

VB.NET浮动窗体

2010-01-22 13:16:05

VB.NET初始化数组

2011-03-30 15:16:27

VB.NET.NETXML

2010-01-13 16:45:44

VB.NET删除控件

2010-01-11 15:31:04

VB.NET拖动窗体

2009-11-03 13:16:58

VB.NET读取文件

2010-01-12 17:02:54

VB.NET文件上传

2010-01-08 18:31:45

VB.NET历史菜单

2010-01-22 16:27:19

VB.NET关于对话框

2010-01-15 19:24:42

2010-01-19 15:30:44

VB.NET比较运算符

2010-01-07 10:02:53

Flash控制VB.N
点赞
收藏

51CTO技术栈公众号