vb.net 教程 4-10 XML文件操作 4-3

前端之家收集整理的这篇文章主要介绍了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 教程 目录

原文链接:https://www.f2er.com/vb/256200.html

猜你在找的VB相关文章