序列化是将对象状态转换为可保存或可传输格式的处理序。序列化的反面是还原序列化,它可以将数据流转换成对象。在 .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 类别的完整程序代码如下
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Namespace [Namespace [Lib]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 物件序列化与反序化共享函式。
''' </summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Class TBSerializerUtilClass TBSerializerUtil
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将对象序列化为 XML 档案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">文件名称。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sPath = System.IO.Path.GetDirectoryName(FileName)
If Not System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End If
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer = New XmlSerializer(Value.GetType())
oStream = New StreamWriter(FileName)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer.Serialize(oStream, Value)
oStream.Close()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将 XML 档案反序列化为对象。
''' </summary>
''' <param name="FileName">文件名称。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'若指定的档案不存在则离开
If Not System.IO.File.Exists(FileName) Then Return Nothing
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer = New XmlSerializer(Type)
oFileStream = New FileStream(FileName, FileMode.Open)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.Deserialize(oFileStream)
oFileStream.Close()
Return oValue
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将对象序列化为 XML 字符串。
''' </summary>
''' <param name="Value">物件。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
olSerializer = New XmlSerializer(Value.GetType())
oMemoryStream = New MemoryStream()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
olSerializer.Serialize(oMemoryStream, Value)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oMemoryStream.Position = 0
oReader = New StreamReader(oMemoryStream)
sText = oReader.ReadToEnd()
Return sText
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将 XML 字符串反序列化为对象。
''' </summary>
''' <param name="Text">XML 字符串。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer = New XmlSerializer(Type)
oBuffer = System.Text.Encoding.UTF8.GetBytes(Text)
oMemoryStream = New MemoryStream(oBuffer)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.Deserialize(oMemoryStream)
Return oValue
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将对象序列化为 Soap 档案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">文件名称。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sPath = System.IO.Path.GetDirectoryName(FileName)
If Not System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End If
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oFormatter = New SoapFormatter()
oFileStream = New FileStream(FileName, FileMode.Create)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oFormatter.Serialize(oFileStream, Value)
oFileStream.Close()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将 Soap 档案反序列化为对象。
''' </summary>
''' <param name="FileName">文件名称。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'若指定的档案不存在则离开
If Not System.IO.File.Exists(FileName) Then Return Nothing
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oFormatter = New SoapFormatter()
oFileStream = New FileStream(FileName, FileMode.Open)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Call the Deserialize method and cast to the object type.
oValue = oFormatter.Deserialize(oFileStream)
oFileStream.Close()
Return oValue
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将对象序列化为 Soap 字符串。
''' </summary>
''' <param name="Value">物件。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oFormatter = New SoapFormatter()
oMemoryStream = New MemoryStream()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oFormatter.Serialize(oMemoryStream, Value)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oMemoryStream.Position = 0
oReader = New StreamReader(oMemoryStream)
sText = oReader.ReadToEnd()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Return sText
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将 Soap 字符串反序列化为对象。
''' </summary>
''' <param name="Text">Soap 字符串。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oFormatter = New SoapFormatter()
oBuffer = System.Text.Encoding.UTF8.GetBytes(Text)
oMemoryStream = New MemoryStream(oBuffer)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Call the Deserialize method and cast to the object type.
oValue = oFormatter.Deserialize(oMemoryStream)
Return oValue
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将对象序列化为 Binary 档案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">文件名称。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sPath = System.IO.Path.GetDirectoryName(FileName)
If Not System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End If
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oFormatter = New BinaryFormatter()
oFileStream = New FileStream(FileName, FileMode.Create)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oFormatter.Serialize(oFileStream, Value)
oFileStream.Close()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将 Binary 档案反序列化为对象。
''' </summary>
''' <param name="FileName">文件名称。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'若指定的档案不存在则离开
If Not System.IO.File.Exists(FileName) Then Return Nothing
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oFormatter = New BinaryFormatter()
oFileStream = New FileStream(FileName, FileMode.Open)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Call the Deserialize method and cast to the object type.
oValue = oFormatter.Deserialize(oFileStream)
oFileStream.Close()
Return oValue
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将对象序列化为 Binary 数据。
''' </summary>
''' <param name="Value">物件。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shared Function ObjectToBinary()Function ObjectToBinary(ByVal Value As Object) As Byte()
Dim oFormatter As SoapFormatter
Dim oMemoryStream As MemoryStream
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oFormatter = New SoapFormatter()
oMemoryStream = New MemoryStream()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oFormatter.Serialize(oMemoryStream, Value)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oMemoryStream.Position = 0
Return oMemoryStream.GetBuffer()
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将 Binary 数据反序列化为对象。
''' </summary>
''' <param name="Buffer">Binary 资料。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oFormatter = New SoapFormatter()
oMemoryStream = New MemoryStream(Buffer)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Call the Deserialize method and cast to the object type.
oValue = oFormatter.Deserialize(oMemoryStream)
Return oValue
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将对象序列化为 Json 档案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">文件名称。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sPath = System.IO.Path.GetDirectoryName(FileName)
If Not System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End If
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer = New DataContractJsonSerializer(Value.GetType())
oFileStream = New FileStream(FileName, FileMode.Create)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer.WriteObject(oFileStream, Value)
oFileStream.Close()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将 Json 档案反序列化为对象。
''' </summary>
''' <param name="FileName">文件名称。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'若指定的档案不存在则离开
If Not System.IO.File.Exists(FileName) Then Return Nothing
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer = New DataContractJsonSerializer(Type)
oFileStream = New FileStream(FileName, FileMode.Open)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.ReadObject(oFileStream)
oFileStream.Close()
Return oValue
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将对象序列化为 Json 字符串。
''' </summary>
''' <param name="Value">物件。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer = New DataContractJsonSerializer(Value.GetType)
oMemoryStream = New MemoryStream()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer.WriteObject(oMemoryStream, Value)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oMemoryStream.Position = 0
oReader = New StreamReader(oMemoryStream)
sText = oReader.ReadToEnd()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Return sText
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将 Json 字符串反序列化为对象。
''' </summary>
''' <param name="Text">Json 字符串。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer = New DataContractJsonSerializer(Type)
oBuffer = System.Text.Encoding.UTF8.GetBytes(Text)
oMemoryStream = New MemoryStream(oBuffer)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.ReadObject(oMemoryStream)
Return oValue
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将对象序列化为 DataContract 档案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">文件名称。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sPath = System.IO.Path.GetDirectoryName(FileName)
If Not System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End If
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer = New DataContractSerializer(Value.GetType())
oFileStream = New FileStream(FileName, FileMode.Create)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer.WriteObject(oFileStream, Value)
oFileStream.Close()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将 DataContract 档案反序列化为对象。
''' </summary>
''' <param name="FileName">文件名称。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'若指定的档案不存在则离开
If Not System.IO.File.Exists(FileName) Then Return Nothing
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer = New DataContractSerializer(Type)
oFileStream = New FileStream(FileName, FileMode.Open)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.ReadObject(oFileStream)
oFileStream.Close()
Return oValue
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将对象序列化为 DataContract 字符串。
''' </summary>
''' <param name="Value">物件。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer = New DataContractSerializer(Value.GetType)
oMemoryStream = New MemoryStream()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer.WriteObject(oMemoryStream, Value)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oMemoryStream.Position = 0
oReader = New StreamReader(oMemoryStream)
sText = oReader.ReadToEnd()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Return sText
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 将 DataContract 字符串反序列化为对象。
''' </summary>
''' <param name="Text">Json 字符串。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oSerializer = New DataContractSerializer(Type)
oBuffer = System.Text.Encoding.UTF8.GetBytes(Text)
oMemoryStream = New MemoryStream(oBuffer)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.ReadObject(oMemoryStream)
Return oValue
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End Class
End Namespace
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)