-
vb.net 教程 4-10 XML文件操作 4-3
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
添加数据由于要调用到别的窗体,所以先来看“增加检测地址”窗体中的代码:
设置3个公共变量,此窗体关闭时需要传递给主窗体:
Public addrName As String
Public addrInfo As String
Public addrType As String
窗口载入时:
Private Sub FormAddr_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler rbPC.CheckedChanged, AddressOf clickrbPC
AddHandler rbWww.CheckedChanged, AddressOf clickrbWww
txtName.Focus()
End Sub
Private Sub clickrbPC(ByVal sender As Object, ByVal e As System.EventArgs)
If rbPC.Checked = True Then
If txtAddr.Text.Length >= 7 Then
If txtAddr.Text.Trim.Substring(0, 7) = "http://" Then
txtAddr.Text = txtAddr.Text.Substring(7, txtAddr.Text.Length - 7)
End If
End If
End If
End Sub
Private Sub clickrbWww(ByVal sender As Object, ByVal e As System.EventArgs)
If rbWww.Checked = True Then
If txtAddr.Text.Length > 7 Then
If txtAddr.Text.Trim.Substring(0, 7) <> "http://" Then
txtAddr.Text = "http://" & txtAddr.Text
End If
Else
txtAddr.Text = "http://" & txtAddr.Text
End If
End If
End Sub
按下确定按钮
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim errMsg As String = checkAddr()
If errMsg = "" Then
addrName = txtName.Text.Trim
addrInfo = txtAddr.Text.Trim
If rbPC.Checked = True Then addrType = "PC" Else addrType = "WWW"
Me.Close()
Me.DialogResult = Windows.Forms.DialogResult.OK
Else
MessageBox.Show(errMsg)
Exit Sub
End If
End Sub
Private Function checkAddr() As String
If txtName.Text.Trim = "" Then
Return "名称不能为空"
End If
If txtAddr.Text.Trim = "" Then
Return "地址不能为空"
End If
Return ""
End Function
按下“取消”按钮:
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Sub
主窗体中的“增加地址”:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim addrname As String = ""
Dim addrinfo As String = ""
Dim addrtype As String = ""
Dim frmAddr As New FormAddr
If frmAddr.ShowDialog <> Windows.Forms.DialogResult.OK Then
frmAddr.Dispose()
Exit Sub
End If
addrname = frmAddr.addrName
addrinfo = frmAddr.addrInfo
addrtype = frmAddr.addrType
Dim xdoc As New XmlDocument()
Try
xdoc.Load(xmlfile)
Dim addrNodes As XmlNode
addrNodes = xdoc.SelectSingleNode("//addrdata")
Dim xmlEleParent As XmlElement = xdoc.CreateElement("addrlist")
Dim xmlEleChildName As XmlElement = xdoc.CreateElement("name")
xmlEleChildName.InnerText = addrname
xmlEleParent.AppendChild(xmlEleChildName)
Dim xmlEleChildaddr As XmlElement = xdoc.CreateElement("addr")
xmlEleChildaddr.InnerText = addrinfo
xmlEleParent.AppendChild(xmlEleChildaddr)
Dim xmlEleChildtype As XmlElement = xdoc.CreateElement("type")
xmlEleChildtype.InnerText = addrtype
xmlEleParent.AppendChild(xmlEleChildtype)
addrNodes.AppendChild(xmlEleParent)
xdoc.Save(xmlfile)
Catch ex As Exception
tsslInfo.Text = "增加地址失败:" & ex.Message
End Try
frmAddr.Dispose()
lvCheck.Items.Clear()
'载入xml中的数据
If loadXmlData() = False Then
Exit Sub
End If
End Sub
主窗体中的“删除地址”按钮:
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
If lvCheck.SelectedItems.Count < 1 Then
tsslInfo.Text = "请先选择一项"
Exit Sub
End If
Dim lvAddrname As String
Dim lvAddrInfo As String
Dim lvAddrType As String
lvAddrname = lvCheck.SelectedItems(0).SubItems(1).Text
lvAddrInfo = lvCheck.SelectedItems(0).SubItems(3).Text
lvAddrType = lvCheck.SelectedItems(0).SubItems(2).Text
Dim xd As New XmlDocument()
Try
xd.Load(xmlfile)
Dim rootNode As XmlNode = xd.SelectSingleNode("//addrdata")
Dim addrNodes As XmlNodeList
addrNodes = rootNode.ChildNodes 'xd.SelectNodes("//addrdata/addrlist")
Dim i As Integer = 0
Dim addrname As String = ""
Dim addrinfo As String = ""
Dim addrtype As String = ""
If addrNodes.Count > 0 Then
For Each addrNode As XmlNode In addrNodes
i += 1
Dim addrNodeInfos As XmlNodeList
addrNodeInfos = addrNode.ChildNodes
addrname = addrNodeInfos(0).InnerText
addrinfo = addrNodeInfos(1).InnerText
addrtype = addrNodeInfos(2).InnerText
If (addrname = lvAddrname) And (addrinfo = lvAddrInfo) And (addrtype = lvAddrType) Then
rootNode.RemoveChild(addrNode)
End If
Next
End If
xd.Save(xmlfile)
lvCheck.Items.Clear()
'载入xml中的数据
If loadXmlData() = False Then
Exit Sub
End If
Catch ex As Exception
tsslInfo.Text = "删除地址失败:" & ex.Message
End Try
End Sub
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录
————————————————
版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/uruseibest/article/details/79028391
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比