序列化是将对象状态转换为可保存或可传输格式的处理序。序列化的反面是还原序列化,它可以将数据流转换成对象。在 .NET 程序中常用的序列化方式如下表所示。
序列化类别 |
说明 |
.NET Framework 版本 |
XmlSerializer |
XML 序列化 |
2.0 |
SoapFormatter |
SOAP 序列化 |
2.0 |
BinaryFormatter |
二进制序列化 |
2.0 |
DataContractSerializer |
资料合约序列化 |
3.5 |
DataContractJsonSerializer |
JSON 序列化 |
3.5 |
我们撰写一个 TBSerializerUtil 类别,来处理上述几种序列化,TBSerializerUtil 类别包含的序列化方法如下表所示。
方法 |
序列化类别 |
说明 |
ObjectToXmlFile |
XmlSerializer |
将对象序列化为 XML 档案 |
XmlFileToObject |
XmlSerializer |
将 XML 档案反序列化为对象 |
ObjectToXml |
XmlSerializer |
将对象序列化为 XML 字符串 |
XmlToObject |
XmlSerializer |
将 XML 字符串反序列化为对象 |
ObjectToSoapFile |
SoapFormatter |
将对象序列化为 Soap 档案 |
SoapFileToObject |
SoapFormatter |
将 Soap 档案反序列化为对象 |
ObjectToSoap |
SoapFormatter |
将对象序列化为 Soap 字符串 |
SoapToObject |
SoapFormatter |
将 Soap 字符串反序列化为对象 |
ObjectToBinaryFile |
BinaryFormatter |
将对象序列化为 Binary 档案 |
BinaryFileToObject |
BinaryFormatter |
将 Binary 档案反序列化为对象 |
ObjectToBinary |
BinaryFormatter |
将对象序列化为 Binary 数据 |
BinaryToObject |
BinaryFormatter |
将 Binary 数据反序列化为对象 |
ObjectToDataContractFile |
DataContractSerializer |
将对象序列化为 DataContract 档案 |
DataContractFileToObject |
DataContractSerializer |
将 DataContract 档案反序列化为对象 |
ObjectToDataContract |
DataContractSerializer |
将对象序列化为 DataContract 字符串 |
DataContractToObject |
DataContractSerializer |
将 DataContract 字符串反序列化为对象 |
ObjectToJsonFile |
DataContractJsonSerializer |
将对象序列化为 Json 档案 |
JsonFileToObject |
DataContractJsonSerializer |
将 Json 档案反序列化为对象 |
ObjectToJson |
DataContractJsonSerializer |
将对象序列化为 Json 字符串 |
JsonToObject |
DataContractJsonSerializer |
将 Json 字符串反序列化为对象 |
TBSerializerUtil 类别的完整程序代码如下
Code
Imports System.Xml.Serialization
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Soap
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.Json
Imports System.Runtime.Serialization
Namespace [Namespace [Lib]
/**/''' <summary>
''' 物件序列化与反序化共享函式。
''' </summary>
Public Class TBSerializerUtilClass TBSerializerUtil
/**/''' <summary>
''' 将对象序列化为 XML 档案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">文件名称。</param>
Public Shared Sub ObjectToXmlFile()Sub ObjectToXmlFile(ByVal Value As Object, ByVal FileName As String)
Dim oSerializer As XmlSerializer
Dim oStream As StreamWriter
Dim sPath As String
sPath = System.IO.Path.GetDirectoryName(FileName)
If Not System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End If
oSerializer = New XmlSerializer(Value.GetType())
oStream = New StreamWriter(FileName)
oSerializer.Serialize(oStream, Value)
oStream.Close()
End Sub
/**/''' <summary>
''' 将 XML 档案反序列化为对象。
''' </summary>
''' <param name="FileName">文件名称。</param>
Public Shared Function XmlFileToObject()Function XmlFileToObject(ByVal FileName As String, ByVal Type As System.Type) As Object
Dim oSerializer As XmlSerializer
Dim oFileStream As FileStream
Dim oValue As Object
'若指定的档案不存在则离开
If Not System.IO.File.Exists(FileName) Then Return Nothing
oSerializer = New XmlSerializer(Type)
oFileStream = New FileStream(FileName, FileMode.Open)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.Deserialize(oFileStream)
oFileStream.Close()
Return oValue
End Function
/**/''' <summary>
''' 将对象序列化为 XML 字符串。
''' </summary>
''' <param name="Value">物件。</param>
Public Shared Function ObjectToXml()Function ObjectToXml(ByVal Value As Object) As String
Dim olSerializer As XmlSerializer
Dim oMemoryStream As MemoryStream
Dim oReader As StreamReader
Dim sText As String
olSerializer = New XmlSerializer(Value.GetType())
oMemoryStream = New MemoryStream()
olSerializer.Serialize(oMemoryStream, Value)
oMemoryStream.Position = 0
oReader = New StreamReader(oMemoryStream)
sText = oReader.ReadToEnd()
Return sText
End Function
/**/''' <summary>
''' 将 XML 字符串反序列化为对象。
''' </summary>
''' <param name="Text">XML 字符串。</param>
Public Shared Function XmlToObject()Function XmlToObject(ByVal Text As String, ByVal Type As System.Type) As Object
Dim oSerializer As XmlSerializer
Dim oMemoryStream As MemoryStream
Dim oBuffer As Byte()
Dim oValue As Object
oSerializer = New XmlSerializer(Type)
oBuffer = System.Text.Encoding.UTF8.GetBytes(Text)
oMemoryStream = New MemoryStream(oBuffer)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.Deserialize(oMemoryStream)
Return oValue
End Function
/**/''' <summary>
''' 将对象序列化为 Soap 档案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">文件名称。</param>
Public Shared Sub ObjectToSoapFile()Sub ObjectToSoapFile(ByVal Value As Object, ByVal FileName As String)
Dim oFormatter As SoapFormatter
Dim oFileStream As FileStream
Dim sPath As String
sPath = System.IO.Path.GetDirectoryName(FileName)
If Not System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End If
oFormatter = New SoapFormatter()
oFileStream = New FileStream(FileName, FileMode.Create)
oFormatter.Serialize(oFileStream, Value)
oFileStream.Close()
End Sub
/**/''' <summary>
''' 将 Soap 档案反序列化为对象。
''' </summary>
''' <param name="FileName">文件名称。</param>
Public Shared Function SoapFileToObject()Function SoapFileToObject(ByVal FileName As String, ByVal Type As System.Type) As Object
Dim oFormatter As SoapFormatter
Dim oFileStream As FileStream
Dim oValue As Object
'若指定的档案不存在则离开
If Not System.IO.File.Exists(FileName) Then Return Nothing
oFormatter = New SoapFormatter()
oFileStream = New FileStream(FileName, FileMode.Open)
'Call the Deserialize method and cast to the object type.
oValue = oFormatter.Deserialize(oFileStream)
oFileStream.Close()
Return oValue
End Function
/**/''' <summary>
''' 将对象序列化为 Soap 字符串。
''' </summary>
''' <param name="Value">物件。</param>
Public Shared Function ObjectToSoap()Function ObjectToSoap(ByVal Value As Object) As String
Dim oFormatter As SoapFormatter
Dim oMemoryStream As MemoryStream
Dim oReader As StreamReader
Dim sText As String
oFormatter = New SoapFormatter()
oMemoryStream = New MemoryStream()
oFormatter.Serialize(oMemoryStream, Value)
oMemoryStream.Position = 0
oReader = New StreamReader(oMemoryStream)
sText = oReader.ReadToEnd()
Return sText
End Function
/**/''' <summary>
''' 将 Soap 字符串反序列化为对象。
''' </summary>
''' <param name="Text">Soap 字符串。</param>
Public Shared Function SoapToObject()Function SoapToObject(ByVal Text As String, ByVal Type As System.Type) As Object
Dim oFormatter As SoapFormatter
Dim oMemoryStream As MemoryStream
Dim oBuffer As Byte()
Dim oValue As Object
oFormatter = New SoapFormatter()
oBuffer = System.Text.Encoding.UTF8.GetBytes(Text)
oMemoryStream = New MemoryStream(oBuffer)
'Call the Deserialize method and cast to the object type.
oValue = oFormatter.Deserialize(oMemoryStream)
Return oValue
End Function
/**/''' <summary>
''' 将对象序列化为 Binary 档案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">文件名称。</param>
Public Shared Sub ObjectToBinaryFile()Sub ObjectToBinaryFile(ByVal Value As Object, ByVal FileName As String)
Dim oFormatter As BinaryFormatter
Dim oFileStream As FileStream
Dim sPath As String
sPath = System.IO.Path.GetDirectoryName(FileName)
If Not System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End If
oFormatter = New BinaryFormatter()
oFileStream = New FileStream(FileName, FileMode.Create)
oFormatter.Serialize(oFileStream, Value)
oFileStream.Close()
End Sub
/**/''' <summary>
''' 将 Binary 档案反序列化为对象。
''' </summary>
''' <param name="FileName">文件名称。</param>
Public Shared Function BinaryFileToObject()Function BinaryFileToObject(ByVal FileName As String, ByVal Type As System.Type) As Object
Dim oFormatter As BinaryFormatter
Dim oFileStream As FileStream
Dim oValue As Object
'若指定的档案不存在则离开
If Not System.IO.File.Exists(FileName) Then Return Nothing
oFormatter = New BinaryFormatter()
oFileStream = New FileStream(FileName, FileMode.Open)
'Call the Deserialize method and cast to the object type.
oValue = oFormatter.Deserialize(oFileStream)
oFileStream.Close()
Return oValue
End Function
/**/''' <summary>
''' 将对象序列化为 Binary 数据。
''' </summary>
''' <param name="Value">物件。</param>
Public Shared Function ObjectToBinary()Function ObjectToBinary(ByVal Value As Object) As Byte()
Dim oFormatter As SoapFormatter
Dim oMemoryStream As MemoryStream
oFormatter = New SoapFormatter()
oMemoryStream = New MemoryStream()
oFormatter.Serialize(oMemoryStream, Value)
oMemoryStream.Position = 0
Return oMemoryStream.GetBuffer()
End Function
/**/''' <summary>
''' 将 Binary 数据反序列化为对象。
''' </summary>
''' <param name="Buffer">Binary 资料。</param>
Public Shared Function BinaryToObject()Function BinaryToObject(ByVal Buffer As Byte(), ByVal Type As System.Type) As Object
Dim oFormatter As SoapFormatter
Dim oMemoryStream As MemoryStream
Dim oValue As Object
oFormatter = New SoapFormatter()
oMemoryStream = New MemoryStream(Buffer)
'Call the Deserialize method and cast to the object type.
oValue = oFormatter.Deserialize(oMemoryStream)
Return oValue
End Function
/**/''' <summary>
''' 将对象序列化为 Json 档案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">文件名称。</param>
Public Shared Sub ObjectToJsonFile()Sub ObjectToJsonFile(ByVal Value As Object, ByVal FileName As String)
Dim oSerializer As DataContractJsonSerializer
Dim oFileStream As FileStream
Dim sPath As String
sPath = System.IO.Path.GetDirectoryName(FileName)
If Not System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End If
oSerializer = New DataContractJsonSerializer(Value.GetType())
oFileStream = New FileStream(FileName, FileMode.Create)
oSerializer.WriteObject(oFileStream, Value)
oFileStream.Close()
End Sub
/**/''' <summary>
''' 将 Json 档案反序列化为对象。
''' </summary>
''' <param name="FileName">文件名称。</param>
Public Shared Function JsonFileToObject()Function JsonFileToObject(ByVal FileName As String, ByVal Type As System.Type) As Object
Dim oSerializer As DataContractJsonSerializer
Dim oFileStream As FileStream
Dim oValue As Object
'若指定的档案不存在则离开
If Not System.IO.File.Exists(FileName) Then Return Nothing
oSerializer = New DataContractJsonSerializer(Type)
oFileStream = New FileStream(FileName, FileMode.Open)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.ReadObject(oFileStream)
oFileStream.Close()
Return oValue
End Function
/**/''' <summary>
''' 将对象序列化为 Json 字符串。
''' </summary>
''' <param name="Value">物件。</param>
Public Shared Function ObjectToJson()Function ObjectToJson(ByVal Value As Object) As String
Dim oSerializer As DataContractJsonSerializer
Dim oMemoryStream As MemoryStream
Dim oReader As StreamReader
Dim sText As String
oSerializer = New DataContractJsonSerializer(Value.GetType)
oMemoryStream = New MemoryStream()
oSerializer.WriteObject(oMemoryStream, Value)
oMemoryStream.Position = 0
oReader = New StreamReader(oMemoryStream)
sText = oReader.ReadToEnd()
Return sText
End Function
/**/''' <summary>
''' 将 Json 字符串反序列化为对象。
''' </summary>
''' <param name="Text">Json 字符串。</param>
Public Shared Function JsonToObject()Function JsonToObject(ByVal Text As String, ByVal Type As System.Type) As Object
Dim oSerializer As DataContractJsonSerializer
Dim oMemoryStream As MemoryStream
Dim oBuffer As Byte()
Dim oValue As Object
oSerializer = New DataContractJsonSerializer(Type)
oBuffer = System.Text.Encoding.UTF8.GetBytes(Text)
oMemoryStream = New MemoryStream(oBuffer)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.ReadObject(oMemoryStream)
Return oValue
End Function
/**/''' <summary>
''' 将对象序列化为 DataContract 档案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">文件名称。</param>
Public Shared Sub ObjectToDataContractFile()Sub ObjectToDataContractFile(ByVal Value As Object, ByVal FileName As String)
Dim oSerializer As DataContractSerializer
Dim oFileStream As FileStream
Dim sPath As String
sPath = System.IO.Path.GetDirectoryName(FileName)
If Not System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End If
oSerializer = New DataContractSerializer(Value.GetType())
oFileStream = New FileStream(FileName, FileMode.Create)
oSerializer.WriteObject(oFileStream, Value)
oFileStream.Close()
End Sub
/**/''' <summary>
''' 将 DataContract 档案反序列化为对象。
''' </summary>
''' <param name="FileName">文件名称。</param>
Public Shared Function DataContractFileToObject()Function DataContractFileToObject(ByVal FileName As String, ByVal Type As System.Type) As Object
Dim oSerializer As DataContractSerializer
Dim oFileStream As FileStream
Dim oValue As Object
'若指定的档案不存在则离开
If Not System.IO.File.Exists(FileName) Then Return Nothing
oSerializer = New DataContractSerializer(Type)
oFileStream = New FileStream(FileName, FileMode.Open)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.ReadObject(oFileStream)
oFileStream.Close()
Return oValue
End Function
/**/''' <summary>
''' 将对象序列化为 DataContract 字符串。
''' </summary>
''' <param name="Value">物件。</param>
Public Shared Function ObjectToDataContract()Function ObjectToDataContract(ByVal Value As Object) As String
Dim oSerializer As DataContractSerializer
Dim oMemoryStream As MemoryStream
Dim oReader As StreamReader
Dim sText As String
oSerializer = New DataContractSerializer(Value.GetType)
oMemoryStream = New MemoryStream()
oSerializer.WriteObject(oMemoryStream, Value)
oMemoryStream.Position = 0
oReader = New StreamReader(oMemoryStream)
sText = oReader.ReadToEnd()
Return sText
End Function
/**/''' <summary>
''' 将 DataContract 字符串反序列化为对象。
''' </summary>
''' <param name="Text">Json 字符串。</param>
Public Shared Function DataContractToObject()Function DataContractToObject(ByVal Text As String, ByVal Type As System.Type) As Object
Dim oSerializer As DataContractSerializer
Dim oMemoryStream As MemoryStream
Dim oBuffer As Byte()
Dim oValue As Object
oSerializer = New DataContractSerializer(Type)
oBuffer = System.Text.Encoding.UTF8.GetBytes(Text)
oMemoryStream = New MemoryStream(oBuffer)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.ReadObject(oMemoryStream)
Return oValue
End Function
End Class
End Namespace