VB.NET读取XML文件实现技巧分享
创始人
2024-06-19 19:10:49
0

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 

 

 

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

 

 

  1. 'Windows 窗体设计器所必需的  
  2. Private components As System.
    ComponentModel.IContainer  
  3. '注意: 以下过程是 Windows 窗体设计
    器所必需的  
  4. '可以使用 Windows 窗体设计器修改此过程。  
  5. '不要使用代码编辑器修改它。  
  6. Friend WithEvents input As System.
    Windows.Forms.TextBox  
  7. Friend WithEvents outtext As System.
    Windows.Forms.TextBox  
  8. Friend WithEvents Button1 As System.
    Windows.Forms.Button  
  9.  
    Private Sub InitializeComponent()
    Sub InitializeComponent()  
  10. Me.input = New System.Windows.
    Forms.TextBox  
  11. Me.outtext = New System.Windows.
    Forms.TextBox  
  12. Me.Button1 = New System.Windows.
    Forms.Button  
  13. Me.SuspendLayout()  
  14. '  
  15. 'input  
  16. '  
  17. Me.input.Location = New System.
    Drawing.Point(16, 8)  
  18. Me.input.Name = "input" 
  19. Me.input.Size = New System.
    Drawing.Size(464, 21)  
  20. Me.input.TabIndex = 0 
  21. Me.input.Text = "http://127.0.0.1/
    fileup/people.xml" 
  22. '  
  23. 'outtext  
  24. '  
  25. Me.outtext.BackColor = System.
    Drawing.SystemColors.HighlightText  
  26. Me.outtext.BorderStyle = System.
    Windows.Forms.BorderStyle.FixedSingle  
  27. Me.outtext.Location = New 
    System.Drawing.Point(0, 40)  
  28. Me.outtext.Multiline = True 
  29. Me.outtext.Name = "outtext" 
  30. Me.outtext.ReadOnly = True 
  31. Me.outtext.ScrollBars = System.
    Windows.Forms.ScrollBars.Both  
  32. Me.outtext.Size = New System.
    Drawing.Size(624, 472)  
  33. Me.outtext.TabIndex = 1 
  34. Me.outtext.Text = "TextBox2" 
  35. '  
  36. 'Button1  
  37. '  
  38. Me.Button1.Location = New 
    System.Drawing.Point(504, 8)  
  39. Me.Button1.Name = "Button1" 
  40. Me.Button1.Size = New System.
    Drawing.Size(96, 24)  
  41. Me.Button1.TabIndex = 2 
  42. Me.Button1.Text = "读 取" 
  43. '  
  44. 'Form1  
  45. '  
  46. Me.AutoScaleBaseSize = New 
    System.Drawing.Size(6, 14)  
  47. Me.ClientSize = New System.
    Drawing.Size(632, 517)  
  48. Me.Controls.Add(Me.Button1)  
  49. Me.Controls.Add(Me.outtext)  
  50. Me.Controls.Add(Me.input)  
  51. Me.Name = "Form1" 
  52. Me.Text = "Form1" 
  53. Me.ResumeLayout(False)  
  54. End Sub 

 

 

  1. #End Region  
  2. Private Sub Button1_Click()
    Sub Button1_Click(ByVal sender 
    As System.Object, ByVal e As 
    System.EventArgs) Handles 
    Button1.Click  
  3. Dim doc As xmldocument = 
    New xmldocument  
  4. Dim y As String  
  5. doc.Load(input.Text)  
  6. Dim rootnode As XmlElement = 
    doc.DocumentElement  
  7. outtext.Text = "" 
  8. enumeratenode(rootnode, 0)  
  9. End Su 

 

 

  1. Private Sub enumeratenode()
    Sub enumeratenode(ByVal node 
    As XmlNode, ByVal indentval 
    As Integer)  
  2. Dim type As String  
  3. Select Case node.NodeType  
  4. Case XmlNodeType.Element  
  5. type = "元素" 
  6. Case XmlNodeType.Text  
  7. type = "文本" 
  8. Case XmlNodeType.Comment  
  9. type = "注释" 
  10. Case Else  
  11. outtext.AppendText(".")  
  12. End Select 

 

  1. outtext.AppendText(type & "节点找到")  
  2. Select Case node.NodeType  
  3. Case XmlNodeType.Element  
  4. outtext.AppendText(",name=" 
    & node.Name & vbCrLf)  
  5. Case XmlNodeType.Text  
  6. outtext.AppendText(",content=" 
    & node.Value & vbCrLf)  
  7. Case XmlNodeType.Comment  
  8. outtext.AppendText(",content=" 
    & node.Value & vbCrLf)  
  9. Case Else  
  10. outtext.AppendText(".")  
  11. End Select 

 

 

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

 

  1. If node.HasChildNodes Then  
  2. outtext.AppendText
    ("此节点有子节点:" & vbCrLf)  
  3. Dim child As XmlNode  
  4. For Each child In node.ChildNodes  
  5. enumeratenode(child, indentval + 1)  
  6. Next  
  7. End If  
  8. End Sub  
  9. End Class 

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

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...