日期:2010-07-31 浏览次数:20457 次
HOW TO:配置或数据文件的保存 这个原是基于NET2003,其中又用了2005的泛型(OF TConfigInformation),显得不伦不类。现在改为2005的,并取消了接口的引入。
序列化类:
Public Class SerializeHelperClass SerializeHelper(Of T)
Private Sub New()Sub New()
End Sub
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)> _
Public Shared Function ItemToXml()Function ItemToXml(ByVal obj As T) As String
Dim mResult As String = ""
Dim mSerializer As New System.Xml.Serialization.XmlSerializer(GetType(T))
Dim mStringWriter As New System.IO.StringWriter
Using mStringWriter
mSerializer.Serialize(mStringWriter, obj)
mResult = mStringWriter.ToString
mStringWriter.Close()
End Using
Return mResult
End Function
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)> _
Public Shared Function XmlToItem()Function XmlToItem(ByVal xml As String) As T
Dim mSerializer As New System.Xml.Serialization.XmlSerializer(GetType(T))
Dim mStringReader As New System.IO.StringReader(xml)
Return CType(mSerializer.Deserialize(mStringReader), T)
End Function
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)> _
Public Shared Sub ItemToXmlFile()Sub ItemToXmlFile(ByVal filename As String, ByVal obj As T)
Dim XmlWriter As New System.IO.StreamWriter(filename, False)
Using XmlWriter
XmlWriter.Write(ItemToXml(obj))
XmlWriter.Close()
End Using
End Sub
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)> _
Public Shared Function XmlFileToItem()Function XmlFileToItem(ByVal filename As String) As T
Dim XmlReader As New System.IO.StreamReader(filename, System.Text.Encoding.Default)
Dim mObj As T
Using XmlReader
mObj = XmlToItem(XmlReader.ReadToEnd)
XmlReader.Close()
End Using
Return mObj
End Function
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)> _
Public Shared Sub ItemToFormatterFile()Sub ItemToFormatterFile(ByVal filename As String, ByVal formatter As System.Runtime.Serialization.IFormatter, ByVal obj As T)
Dim mFileStream As System.IO.Stream = System.IO.File.Open(filename, System.