📄 form1.vb
字号:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides 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
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.Button3 = New System.Windows.Forms.Button()
Me.Button4 = New System.Windows.Forms.Button()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.AlternatingBackColor = System.Drawing.Color.LightGray
Me.DataGrid1.BackColor = System.Drawing.Color.DarkGray
Me.DataGrid1.CaptionBackColor = System.Drawing.Color.White
Me.DataGrid1.CaptionFont = New System.Drawing.Font("Verdana", 10.0!)
Me.DataGrid1.CaptionForeColor = System.Drawing.Color.Navy
Me.DataGrid1.CaptionText = "用DataGrid显示XML文件"
Me.DataGrid1.DataMember = ""
Me.DataGrid1.ForeColor = System.Drawing.Color.Black
Me.DataGrid1.GridLineColor = System.Drawing.Color.Black
Me.DataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None
Me.DataGrid1.HeaderBackColor = System.Drawing.Color.Silver
Me.DataGrid1.HeaderForeColor = System.Drawing.Color.Black
Me.DataGrid1.LinkColor = System.Drawing.Color.Navy
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.ParentRowsBackColor = System.Drawing.Color.White
Me.DataGrid1.ParentRowsForeColor = System.Drawing.Color.Black
Me.DataGrid1.SelectionBackColor = System.Drawing.Color.Navy
Me.DataGrid1.SelectionForeColor = System.Drawing.Color.White
Me.DataGrid1.Size = New System.Drawing.Size(416, 160)
Me.DataGrid1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 168)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(80, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "列出所有"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(112, 168)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(80, 24)
Me.Button2.TabIndex = 2
Me.Button2.Text = "增加节点"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(216, 168)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(80, 24)
Me.Button3.TabIndex = 3
Me.Button3.Text = "删除节点"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(320, 168)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(80, 24)
Me.Button4.TabIndex = 4
Me.Button4.Text = "新建文件"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(416, 205)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button4, Me.Button3, Me.Button2, Me.Button1, Me.DataGrid1})
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "读写XML文件"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As DataSet = New DataSet("bookstore")
ds.ReadXml("..\MyXMLFile.xml")
DataGrid1.DataSource = ds
DataGrid1.DataMember = "book"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim doc As Xml.XmlDocument = New Xml.XmlDocument()
'载入XML文件
doc.Load("..\MyXMLFile.xml")
'创建一个新的节点
Dim newbook As Xml.XmlElement = doc.CreateElement("book")
'设置属性
newbook.SetAttribute("出版社", "清华大学出版社")
'创建一个新的'书名'元素
Dim newbookname As Xml.XmlElement = doc.CreateElement("书名")
newbookname.InnerText = "ASP动态网站开发教程"
newbook.AppendChild(newbookname)
'创建一个新的'作者'元素
Dim newauthor As Xml.XmlElement = doc.CreateElement("作者")
newauthor.InnerText = "廖杉山"
newbook.AppendChild(newauthor)
'创建一个新的'出版日期'元素
Dim newpublic As Xml.XmlElement = doc.CreateElement("出版日期")
newpublic.InnerText = "2000年11月"
newbook.AppendChild(newpublic)
'创建一个新的'价格'元素
Dim newprice As Xml.XmlElement = doc.CreateElement("价格")
newprice.InnerText = "29.00"
newbook.AppendChild(newprice)
'将新的节点加入到当前的文档中
doc.DocumentElement.AppendChild(newbook)
'写入到硬盘中
Dim tr As Xml.XmlTextWriter = New Xml.XmlTextWriter("..\MyXMLFile.xml", System.Text.Encoding.UTF8)
tr.Formatting = Xml.Formatting.Indented
doc.WriteContentTo(tr)
tr.Close()
MessageBox.Show("增加成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim doc As Xml.XmlDocument = New Xml.XmlDocument()
'载入XML文件
doc.Load("..\MyXMLFile.xml")
'创建一个新的节点
Dim mynode As Xml.XmlNode
Dim root As Xml.XmlNode = doc.DocumentElement
mynode = root.SelectSingleNode("descendant::book[书名='ASP动态网站开发教程']")
root.RemoveChild(mynode)
Dim tr As Xml.XmlTextWriter = New Xml.XmlTextWriter("..\MyXMLFile.xml", System.Text.Encoding.UTF8)
tr.Formatting = Xml.Formatting.Indented
doc.WriteContentTo(tr)
tr.Close()
MessageBox.Show("删除成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim doc As Xml.XmlDocument = New Xml.XmlDocument()
Dim newdec As Xml.XmlDeclaration = doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
doc.AppendChild(newdec)
Dim newroot As Xml.XmlElement = doc.CreateElement("newbookstore")
doc.AppendChild(newroot)
'创建一个新的节点
Dim newbook As Xml.XmlElement = doc.CreateElement("book")
'设置属性
newbook.SetAttribute("出版社", "清华大学出版社")
'创建一个新的'书名'元素
Dim newbookname As Xml.XmlElement = doc.CreateElement("书名")
newbookname.InnerText = "ASP动态网站开发教程"
newbook.AppendChild(newbookname)
'创建一个新的'作者'元素
Dim newauthor As Xml.XmlElement = doc.CreateElement("作者")
newauthor.InnerText = "廖杉山"
newbook.AppendChild(newauthor)
'创建一个新的'出版日期'元素
Dim newpublic As Xml.XmlElement = doc.CreateElement("出版日期")
newpublic.InnerText = "2000年11月"
newbook.AppendChild(newpublic)
'创建一个新的'价格'元素
Dim newprice As Xml.XmlElement = doc.CreateElement("价格")
newprice.InnerText = "29.00"
newbook.AppendChild(newprice)
'将新的节点加入到当前的文档中
doc.DocumentElement.AppendChild(newbook)
'写入到硬盘中
Dim tr As Xml.XmlTextWriter = New Xml.XmlTextWriter("books.xml", System.Text.Encoding.UTF8)
tr.Formatting = Xml.Formatting.Indented
doc.WriteContentTo(tr)
tr.Close()
MessageBox.Show("新建文件完成!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As DataSet = New DataSet("bookstore")
ds.ReadXml("..\MyXMLFile.xml")
DataGrid1.DataSource = ds
DataGrid1.DataMember = "book"
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -