address类用于存储地址簿中条目的一个实例。
AddressBook类用于存储所有的条目信息
System.Xml.Serialization 命名空间
https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.serialization?view=netframework-4.8
命名空间中的中心类是XmlSerializer类
XmlSerializer 类
Stream 类
https://docs.microsoft.com/zh-cn/dotnet/api/system.io.stream?view=netframework-4.8
FileStream 类
https://docs.microsoft.com/zh-cn/dotnet/api/system.io.filestream?view=netframework-4.8
FileInfo 类
https://docs.microsoft.com/zh-cn/dotnet/api/system.io.fileinfo?view=netframework-4.8
界面
后台
Public Class Form1
Public AddressBook As AddressBook
Private _currentAddressIndex As Integer
'当前Address索引
Property CurrentAddressindex() As Integer
Get
Return _currentAddressIndex
End Get
Set(ByVal value As Integer)
_currentAddressIndex = value
populateFormFromAddress(CurrentAddress)
lblAddressNumber.Text = _currentAddressIndex & " Of " & AddressBook.Items.Count
End Set
End Property
'当前Address
ReadOnly Property CurrentAddress() As Address
Get
'AddressBook.Items是ArrayList,编号从0开始
Return AddressBook.Items(CurrentAddressindex - 1)
End Get
End Property
'设置文件夹
Public ReadOnly Property DataFileName() As String
Get
Dim folder As String
'返回addressBook程序的文件夹
folder = Environment.CurrentDirectory
Return folder & "\Addressbook.xml"
End Get
End Property
'从窗体中获取Address对象值
Public Sub PopulateAddressFromForm(ByVal address As Address)
address.FirstName = txtFirstName.Text
address.LastName = txtLastName.Text
address.CompanyName = txtCompanyName.Text
address.Address1 = txtAddress1.Text
address.Address2 = txtAddress2.Text
address.City = txtCity.Text
address.Region = txtRegion.Text
address.PostalCode = txtPostalCode.Text
address.Country = txtCountry.Text
address.Email = txtEmail.Text
End Sub
'窗体控件读入Address对象
Public Sub populateFormFromAddress(ByVal address As Address)
txtFirstName.Text = address.FirstName
txtLastName.Text = address.LastName
txtCompanyName.Text = address.CompanyName
txtAddress1.Text = address.Address1
txtAddress2.Text = address.Address2
txtCity.Text = address.City
txtRegion.Text = address.Region
txtPostalCode.Text = address.PostalCode
txtCountry.Text = address.Country
txtEmail.Text = address.Email
End Sub
'保存修改
Public Sub SaveChanges()
AddressBook.Save(DataFileName)
End Sub
'更新当前记录
Private Sub UpdateCurrentAddress()
PopulateAddressFromForm(CurrentAddress)
End Sub
'保存新记录
Public Function AddNewAddress() As Address
UpdateCurrentAddress()
Dim NewAddress As Address = AddressBook.AddAddress
CurrentAddressindex = AddressBook.Items.Count
Return NewAddress
End Function
'----------------------------------------------------------------
'保存按钮
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
'创建一个address对象
Dim address As New Address
'读取窗体文本框,填充address的对象中的属性
PopulateAddressFromForm(address)
'给出一个文件名
Dim filename As String = DataFileName
'调用保存方法
address.Save(filename)
MsgBox("The address was saved to " & filename)
End Sub
'加载按钮
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Dim newAddress As Address = SerializableData.Load(DataFileName, GetType(Address))
populateFormFromAddress(newAddress)
End Sub
'发送Email链接
Private Sub lnkSendEmail_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkSendEmail.LinkClicked
System.Diagnostics.Process.Start("mailto:" & txtEmail.Text)
End Sub
'窗体关闭事件
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
updateCurrentAddress()
SaveChanges()
End Sub
'窗体加载
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddressBook = SerializableData.Load(DataFileName, GetType(AddressBook))
If AddressBook.Items.Count = 0 Then
AddressBook.AddAddress()
End If
CurrentAddressindex = 1
End Sub
'新建记录按钮
Private Sub bntNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntNew.Click
addNewAddress
End Sub
'下一个记录按钮
Private Sub btnNex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNex.Click
MoveNext()
End Sub
'记录后移
Public Sub MoveNext()
Dim newIndex As Integer = CurrentAddressindex + 1
If newIndex > AddressBook.Items.Count Then
newIndex = 1
End If
UpdateCurrentAddress()
CurrentAddressindex = newIndex
End Sub
'前一个记录按钮
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
MovePrevious()
End Sub
'记录前移一个
Public Sub MovePrevious()
Dim newIndex As Integer = CurrentAddressindex - 1
If newIndex = 0 Then
newIndex = AddressBook.Items.Count
End If
UpdateCurrentAddress()
CurrentAddressindex = newIndex
End Sub
'删除按钮
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MsgBox("Are you sure? ", MsgBoxStyle.Question Or MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
DeleteAddress(CurrentAddressindex)
End If
End Sub
'删除记录
Public Sub Deleteaddress(ByVal index As Integer)
AddressBook.Items.RemoveAt(index - 1)
If AddressBook.Items.Count = 0 Then
AddressBook.AddAddress()
Else
If index > AddressBook.Items.Count Then
index = AddressBook.Items.Count
End If
End If
CurrentAddressindex = index
End Sub
End Class
附自定义的三个类
-------------------------------------------------------------------------
‘
Imports System.IO
Imports System.Xml.Serialization
Public Class SerializableData
'读取文件名并打开文件
Public Sub Save(ByVal filename As String)
'创建一个临时文件名
Dim tempFilename As String
tempFilename = filename & ".tmp"
'判断文件是不是存在,存在则删除
'FileInfo类提供用于创建、复制、删除、移动和打开文件的属性和实例方法,并且帮助创建 FileStream 对象。
Dim tempFileInfo As New FileInfo(tempFilename)
If tempFileInfo.Exists = True Then tempFileInfo.Delete()
'创建文件
'FileStream类使用指定的路径和创建模式初始化 FileStream 类的新实例。
Dim stream As New FileStream(tempFilename, FileMode.Create)
'保存文件
Save(stream)
'关闭文件
stream.Close()
'移动文件
tempFileInfo.CopyTo(filename, True)
tempFileInfo.Delete()
End Sub
'用XmlSerializer类保存数据
'Sream类提供字节序列的一般视图
Public Sub Save(ByVal stream As Stream)
'创建一个XmlSerializer对象,控制如何将对象编码到 XML 中。GetType方法返回System.Type对象
'它的参数是一个Address类
Dim serializer As New XmlSerializer(Me.GetType)
serializer.Serialize(stream, Me)
End Sub
'按文件名载入文件
Public Shared Function Load(ByVal filename As String, ByVal newType As Type) As Object
Dim fileInfo As New FileInfo(filename)
'文件不存在返回空对象
If fileInfo.Exists = False Then
'Activator类用以在本地或从远程创建对象类型,或获取对现有远程对象的引用
Return System.Activator.CreateInstance(newType)
End If
'文件存在,则以XML方式载入
Dim stream As New FileStream(filename, FileMode.Open)
'载入Stream
Dim newObject As Object = Load(stream, newType)
stream.Close()
Return newObject
End Function
'load方法重载
Public Shared Function load(ByVal stream As Stream, ByVal newType As Type) As Object
Dim serializer As New XmlSerializer(newType)
Dim newObject As Object = serializer.Deserialize(stream)
Return newObject
End Function
End Class
----------------------------------------------------------------------
’地址类
Public Class Address
Inherits SerializableData
'设置字段
Public FirstName As String
Public LastName As String
Public CompanyName As String
Public Address1 As String
Public Address2 As String
Public City As String
Public Region As String
Public PostalCode As String
Public Country As String
Public Email As String
End Class
-------------------------------------------------------------------------------
‘地址数组类
Imports System.Xml.Serialization
'返回Address对象数组,通知XmlSerializer不处理Items属性,因为Xmlserializer不能处理ArrayList对象
Public Class AddressBook
Inherits SerializableData
'ArrayList编号从0开始
<XmlIgnore()> Public Items As New ArrayList()
Public Property Addresses() As Address() '属性,Address对象数组
Get
Dim addressArray(Items.Count - 1) As Address
Items.CopyTo(addressArray)
Return addressArray
End Get
Set(ByVal value As Address())
Items.Clear()
If Not value Is Nothing Then
Dim address As Address
For Each address In value
Items.Add(address)
Next
End If
End Set
End Property
Public Function AddAddress() As Address
Dim newAddress As New Address
Items.Add(newAddress)
Return newAddress
End Function
End Class
------------------------------------------------------------------------------