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

前端之家收集整理的这篇文章主要介绍了vb.net 教程 4-10 XML文件操作 4-2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

主窗体的代码

需要引用的类:

Imports System.Threading
Imports System.Xml
Imports System.IO
Imports System.Net

窗体级变量:

Dim xmlfile As String	'保存地址的xml文件
    Dim pingTime As Integer     'ping超时时间,毫秒,默认为500
    Dim threadCheck As Thread   '检测线程
    Dim blStopCheck As Boolean  '是否停止检测标记

窗体载入时,载入xml文件中的数据,并显示在ListView控件:

Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CheckForIllegalCrossThreadCalls = False   ‘设置允许窗体和多线程交互

        xmlfile = Application.StartupPath.TrimEnd("\") & "\linked.dat"
        pingTime = 500             ’设置Ping超时时间

        Call localInfo()

        '载入xml中的数据
        If loadXmlData() = False Then
            Exit Sub
        End If
        btnStart.Enabled = True
        btnDel.Enabled = True

        comTime.Text = comTime.Items(2).ToString
    End Sub

获取当前网络状态等信息,并显示到状态栏中:

Private Sub localInfo()
        Dim info As String = ""
        If My.Computer.Network.IsAvailable Then
            info = "网络连接中"
            Dim localname As String
            localname = System.Net.Dns.GetHostName
            Dim localips() As IPAddress
            localips = System.Net.Dns.GetHostByName(localname).AddressList
            Dim localip As String
            If localips.Length > 0 Then
                localip = localips(0).ToString
            Else
                localip = ""
            End If
            info &= ",IP地址:" & localip
        Else
            info = "网络不可用"
        End If

        Dim strAuthor As String = "程序设计:http://blog.csdn.net/uruseibest"
        info = strAuthor & "     " & info
        tsslInfo.Text = info
    End Sub

载入XML中的数据并在ListView中显示

Private Function loadXmlData() As Boolean

        '文件不存在
        If File.Exists(xmlfile) = False Then
            '创建文件
            Call createDatafile()
        End If
        Dim xd As New XmlDocument()
        Try
            xd.Load(xmlfile)
            Dim addrNodes As XmlNodeList
            addrNodes = 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

                    Dim lvAddrInfo As New ListViewItem()
                    lvAddrInfo.Text = i
                    lvAddrInfo.SubItems.Add(addrname)
                    lvAddrInfo.SubItems.Add(addrtype)
                    lvAddrInfo.SubItems.Add(addrinfo)
                    lvAddrInfo.SubItems.Add("等待")
                    lvAddrInfo.SubItems.Add("")
                    lvCheck.Items.Add(lvAddrInfo)

                Next
            End If

        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function


注意,当xml文件不存在的时候,创建一个没有数据的xml文件

Private Sub createDatafile()
        Dim strXmlDefault() As String
        ReDim strXmlDefault(2)
        strXmlDefault(0) = "<?xml version=""1.0"" encoding=""utf-8""?>"
        strXmlDefault(1) = "<addrdata>"
        strXmlDefault(2) = "</addrdata>"
        File.WriteAllLines(xmlfile,strXmlDefault)
    End Sub



由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看vb.net 教程 目录

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

猜你在找的VB相关文章