Visual Basic.NET 7.x (2002/03)Visual Studio .NET 2003.NET 1.1XML中级开发Visual StudioWindows.NETVisual Basic
序列化 CollectionBase 的自定义集合






3.32/5 (16投票s)
关于如何序列化和反序列化自定义集合的基本演示
引言
这是一个简单的演示,展示如何序列化和反序列化自定义集合,同时处理派生集合项。 此版本还演示了序列化、反序列化 cItem
的未知派生类型(参见 Eitem
类)。
背景
我希望能够使用集合而不是数组进行序列化,从而实现更好的类型检查。
Using the Code
此页面没有显示所有代码,但展示了基本结构。 下载应用程序以了解对象的用法演示。
'-----------------------------------------
'The abstract item
'
'NOTE: Notice the attributes on the Citem, these are all the
'derived types we plan to use in the collection
'
Option Explicit On
Option Strict On
Imports System.Xml.Serialization
'NOTE: If you derive from this clas, then add a
'XmlInclude attribute for the new class!
< _
XmlInclude(GetType(Yitem)), _
XmlInclude(GetType(XItem)), _
Serializable() _
> _
Public Class Citem
Private m_Id As Integer
Private m_Data As String
Public Property Id() As Integer
Get
Return m_Id
End Get
Set(ByVal value As Integer)
m_Id = value
End Set
End Property
Public Property Data() As String
Get
Return m_Data
End Get
Set(ByVal value As String)
m_Data = value
End Set
End Property
End Class
'-------------------------------------------
'derived collection item
'This one has an extra property for the demon
'to show differences in XML output
'
Option Explicit On
Option Strict On
Public Class XItem
Inherits cItem
Private m_D As Double
Public Property D() As Double
Get
Return m_D
End Get
Set(ByVal value As Double)
m_D = Value
End Set
End Property
End Class
'-----------------------------------------------
'derived collection item
'
Option Explicit On
Option Strict On
Public Class Yitem
Inherits cItem
End Class
'--------------------------------------------
'derived collection item
'NOTE: XmlInclude attribute is not included on cItem,
'This was done to demo Serialize/Deserialize
'unknown derived types of cItem
Option Explicit On
Option Strict On
Public Class Eitem
Inherits Citem
Private m_B As Double
Public Property B() As Double
Get
Return m_B
End Get
Set(ByVal value As Double)
m_B = value
End Set
End Property
End Class
'---------------------------------------------------
'The Collection
'
Option Explicit On
Option Strict On
Imports System.Xml
Imports System.IO
<Serializable()> _
Public Class MyCollection
Inherits System.Collections.CollectionBase
Public Overridable Function Add(ByVal value As cItem) As Integer
MyBase.List.Add(value)
End Function
Default Public Overridable Property Item(ByVal index As Integer) As cItem
Get
Return DirectCast(MyBase.List.Item(index), cItem)
End Get
Set(ByVal value As cItem)
MyBase.List.Item(index) = value
End Set
End Property
Public Shared Sub SerializeObject(ByVal filename As String, _
ByVal col As MyCollection, ByVal ExtraTypes() As System.Type)
Try
' Create a new XmlSerializer instance.
Dim s As New Xml.Serialization.XmlSerializer(_
GetType(MyCollection), ExtraTypes)
' Writing the XML file to disk requires a TextWriter.
Dim writer As New StreamWriter(filename)
' Serialize the object, and close the StreamWriter.
s.Serialize(writer, col)
writer.Close()
Catch x As System.InvalidOperationException
Throw New Exception("Check class cItem, all derived "+ _
"classes must be listed with the [ "+
"XmlInclude(GetType(derrivedClass)) ] attribute!")
End Try
End Sub
Public Shared Sub SerializeObject(ByVal filename As String, _
ByVal col As MyCollection)
Try
' Create a new XmlSerializer instance.
Dim s As New Xml.Serialization.XmlSerializer(GetType(MyCollection))
' Writing the XML file to disk requires a TextWriter.
Dim writer As New StreamWriter(filename)
' Serialize the object, and close the StreamWriter.
s.Serialize(writer, col)
writer.Close()
Catch x As System.InvalidOperationException
Throw New Exception("Check class cItem, all derived classes "+ _
"must be listed with the [ XmlInclude"+ _
"(GetType(derrivedClass)) ] attribute!")
End Try
End Sub
Public Shared Function DeserializeObject(ByVal filename As String, _
ByVal ExtraTypes() As System.Type) As MyCollection
Try
Dim fs As New IO.FileStream(filename, FileMode.Open)
Dim w As New Xml.Serialization.XmlSerializer( _
GetType(MyCollection), ExtraTypes)
Dim g As MyCollection = CType(w.Deserialize(fs), MyCollection)
fs.Close()
Return g
Catch x As Exception
Throw
End Try
End Function
Public Shared Function DeserializeObject(ByVal filename As String _
) As MyCollection
Try
Dim fs As New IO.FileStream(filename, FileMode.Open)
Dim w As New Xml.Serialization.XmlSerializer(GetType(MyCollection))
Dim g As MyCollection = CType(w.Deserialize(fs), MyCollection)
fs.Close()
Return g
Catch x As Exception
Throw
End Try
End Function
End Class
历史
- 2019年1月31日:初始版本