物件序列化函式库

序列化是将对象状态转换为可保存或可传输格式的处理序。序列化的反面是还原序列化,它可以将数据流转换成对象。在 .NET 程序中常用的序列化方式如下表所示。

序列化类别说明.NET Framework 版本
XmlSerializerXML 序列化2.0
SoapFormatterSOAP 序列化2.0
BinaryFormatter二进制序列化2.0
DataContractSerializer资料合约序列化3.5
DataContractJsonSerializerJSON 序列化3.5

 

我们撰写一个 TBSerializerUtil 类别,来处理上述几种序列化,TBSerializerUtil 类别包含的序列化方法如下表所示。

方法序列化类别说明
ObjectToXmlFileXmlSerializer将对象序列化为 XML 档案
XmlFileToObjectXmlSerializer将 XML 档案反序列化为对象
ObjectToXmlXmlSerializer将对象序列化为 XML 字符串
XmlToObjectXmlSerializer将 XML 字符串反序列化为对象
ObjectToSoapFileSoapFormatter将对象序列化为 Soap 档案
SoapFileToObjectSoapFormatter将 Soap 档案反序列化为对象
ObjectToSoapSoapFormatter将对象序列化为 Soap 字符串
SoapToObjectSoapFormatter将 Soap 字符串反序列化为对象
ObjectToBinaryFileBinaryFormatter将对象序列化为 Binary 档案
BinaryFileToObjectBinaryFormatter将 Binary 档案反序列化为对象
ObjectToBinaryBinaryFormatter将对象序列化为 Binary 数据
BinaryToObjectBinaryFormatter将 Binary 数据反序列化为对象
ObjectToDataContractFileDataContractSerializer将对象序列化为 DataContract 档案
DataContractFileToObjectDataContractSerializer将 DataContract 档案反序列化为对象
ObjectToDataContractDataContractSerializer将对象序列化为 DataContract 字符串
DataContractToObjectDataContractSerializer将 DataContract 字符串反序列化为对象
ObjectToJsonFileDataContractJsonSerializer将对象序列化为 Json 档案
JsonFileToObjectDataContractJsonSerializer将 Json 档案反序列化为对象
ObjectToJsonDataContractJsonSerializer将对象序列化为 Json 字符串
JsonToObjectDataContractJsonSerializer将 Json 字符串反序列化为对象

 

TBSerializerUtil 类别的完整程序代码如下

ContractedBlock.gifExpandedBlockStart.gifCode
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

ExpandedBlockStart.gifContractedBlock.gif
Namespace [Namespace [Lib]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**/''' <summary>
    
''' 物件序列化与反序化共享函式。
    
''' </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif    Public Class TBSerializerUtilClass TBSerializerUtil
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将对象序列化为 XML 档案。
        
''' </summary>
        
''' <param name="Value">物件。</param>
        
''' <param name="FileName">文件名称。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Sub ObjectToXmlFile()Sub ObjectToXmlFile(ByVal Value As ObjectByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将 XML 档案反序列化为对象。
        
''' </summary>
        
''' <param name="FileName">文件名称。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function XmlFileToObject()Function XmlFileToObject(ByVal FileName As StringByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将对象序列化为 XML 字符串。
        
''' </summary>
        
''' <param name="Value">物件。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function ObjectToXml()Function ObjectToXml(ByVal Value As ObjectAs 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将 XML 字符串反序列化为对象。
        
''' </summary>
        
''' <param name="Text">XML 字符串。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function XmlToObject()Function XmlToObject(ByVal Text As StringByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将对象序列化为 Soap 档案。
        
''' </summary>
        
''' <param name="Value">物件。</param>
        
''' <param name="FileName">文件名称。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Sub ObjectToSoapFile()Sub ObjectToSoapFile(ByVal Value As ObjectByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将 Soap 档案反序列化为对象。
        
''' </summary>
        
''' <param name="FileName">文件名称。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function SoapFileToObject()Function SoapFileToObject(ByVal FileName As StringByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将对象序列化为 Soap 字符串。
        
''' </summary>
        
''' <param name="Value">物件。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function ObjectToSoap()Function ObjectToSoap(ByVal Value As ObjectAs 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将 Soap 字符串反序列化为对象。
        
''' </summary>
        
''' <param name="Text">Soap 字符串。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function SoapToObject()Function SoapToObject(ByVal Text As StringByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将对象序列化为 Binary 档案。
        
''' </summary>
        
''' <param name="Value">物件。</param>
        
''' <param name="FileName">文件名称。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Sub ObjectToBinaryFile()Sub ObjectToBinaryFile(ByVal Value As ObjectByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将 Binary 档案反序列化为对象。
        
''' </summary>
        
''' <param name="FileName">文件名称。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function BinaryFileToObject()Function BinaryFileToObject(ByVal FileName As StringByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将对象序列化为 Binary 数据。
        
''' </summary>
        
''' <param name="Value">物件。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function ObjectToBinary()Function ObjectToBinary(ByVal Value As ObjectAs 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将 Binary 数据反序列化为对象。
        
''' </summary>
        
''' <param name="Buffer">Binary 资料。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将对象序列化为 Json 档案。
        
''' </summary>
        
''' <param name="Value">物件。</param>
        
''' <param name="FileName">文件名称。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Sub ObjectToJsonFile()Sub ObjectToJsonFile(ByVal Value As ObjectByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将 Json 档案反序列化为对象。
        
''' </summary>
        
''' <param name="FileName">文件名称。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function JsonFileToObject()Function JsonFileToObject(ByVal FileName As StringByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将对象序列化为 Json 字符串。
        
''' </summary>
        
''' <param name="Value">物件。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function ObjectToJson()Function ObjectToJson(ByVal Value As ObjectAs 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将 Json 字符串反序列化为对象。
        
''' </summary>
        
''' <param name="Text">Json 字符串。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function JsonToObject()Function JsonToObject(ByVal Text As StringByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将对象序列化为 DataContract 档案。
        
''' </summary>
        
''' <param name="Value">物件。</param>
        
''' <param name="FileName">文件名称。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Sub ObjectToDataContractFile()Sub ObjectToDataContractFile(ByVal Value As ObjectByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将 DataContract 档案反序列化为对象。
        
''' </summary>
        
''' <param name="FileName">文件名称。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function DataContractFileToObject()Function DataContractFileToObject(ByVal FileName As StringByVal 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将对象序列化为 DataContract 字符串。
        
''' </summary>
        
''' <param name="Value">物件。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function ObjectToDataContract()Function ObjectToDataContract(ByVal Value As ObjectAs 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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/''' <summary>
        
''' 将 DataContract 字符串反序列化为对象。
        
''' </summary>
        
''' <param name="Text">Json 字符串。</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        Public Shared Function DataContractToObject()Function DataContractToObject(ByVal Text As StringByVal 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

转载于:https://www.cnblogs.com/jeff377/archive/2008/08/19/1271801.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值