您好,登錄后才能下訂單哦!
這篇文章主要介紹“VB.NET怎么讀取XML文件”,在日常操作中,相信很多人在VB.NET怎么讀取XML文件問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”VB.NET怎么讀取XML文件”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
VB.NET未開發人員帶來了不一樣的開發方式。其中特有的各種特點和語言特性為編程人員開發程序提供了很大的幫助。我們今天就來看一段實現VB.NET讀取XML文件的VB代碼。使用了遞歸方式。
VB.NET Socket編程實際操作方法介紹
迅速掌握VB.NET讀取INI文件操作方法
VB.NET Hashtable用法相關概念詳解
VB.NET鍵盤事件相關內容概覽
VB.NET正則表達式應用經驗分享
VB.NET讀取XML文件代碼如下:
Imports System.xml
Public Class Form1Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗體設計器生成的代碼 "
Public Sub New()Sub New()
MyBase.New()
'該調用是 Windows 窗體設計器所必需的。
InitializeComponent()
'在 InitializeComponent()
調用之后添加任何初始化
End Sub
'窗體重寫 dispose 以清理組件列表。
Protected Overloads Overrides
Sub Dispose()Sub Dispose(ByVal
disposing As Boolean)If disposing Then
If Not (components Is Nothing)
Thencomponents.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.TextBoxFriend WithEvents outtext As System.
Windows.Forms.TextBoxFriend WithEvents Button1 As System.
Windows.Forms.Button<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Sub InitializeComponent()Me.input = New System.Windows.
Forms.TextBoxMe.outtext = New System.Windows.
Forms.TextBoxMe.Button1 = New System.Windows.
Forms.ButtonMe.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.HighlightTextMe.outtext.BorderStyle = System.
Windows.Forms.BorderStyle.FixedSingleMe.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.BothMe.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
#End Region
Private Sub Button1_Click()
Sub Button1_Click(ByVal sender
As System.Object, ByVal e As
System.EventArgs) Handles
Button1.ClickDim doc As xmldocument =
New xmldocumentDim y As String
doc.Load(input.Text)
Dim rootnode As XmlElement =
doc.DocumentElementouttext.Text = ""
enumeratenode(rootnode, 0)
End Su
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
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
到此,關于“VB.NET怎么讀取XML文件”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。