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版权协议,转载请附上原文出处链接及本声明。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值