[XML] 利用XML串行化,实现应用程序配置类的存储和读取

.Net 提供了极其方便的Xml串行化和反串行化机制,封装在

System.Xml.Serialization.XmlSerializer 中

当对类做Xml串行化时,将对类中的Public属性或者成员进行串行化操作。

对于一个应用程序的配置类来说,在早期的应用程序中,多采用INI文件

但对于现在而言,INI文件已经显得有些笨拙了,读取和写入并不轻松。

 

有人会说Xml的读取和写入也很复杂,这我同意,但是,应用Xml串行化,

将让你的操作,变得如此简单,如果你亲手试一试的话。

 

我们知道,XmlSerializer在串行化时,需要用户提供 需要串行化类中所有

包含的类型(Type),所以,我所写的Xml串行化扩展 程序集,包含了一个

接口(Interface)

1 Public Interface IXmlSerializationAble 2 Function GetIncludeTypes() As Type() 3 End Interface

这个接口,强调继承它的类,要提供包含的类型,直接的或者间接的。

继承了该接口即可通过我所写的XmlSerializationEx,快速的进行读写,

岂不是,做配置文件读写最方便的方法么?当然,你也可以用于简单的

数据存储。或者数据传递。

 

1 Public NotInheritable Class XmlSerializationEx 2 3 Public Shared Function Read(ByRef instance As IXmlSerializationAble, ByVal fileName As String) As Boolean 4 Dim fi As New IO.FileInfo(fileName) 5 If Not fi.Exists Then 6 Return False 7 End If 8 Dim fs As IO.FileStream = fi.OpenRead 9 Dim reader As New System.Xml.Serialization.XmlSerializer(instance.GetType, instance.GetIncludeTypes) 10 11 Try 12 instance = reader.Deserialize(fs) 13 Catch ex As Exception 14 Return False 15 Finally 16 If Not fs Is Nothing Then fs.Close() 17 reader = Nothing 18 End Try 19 Return True 20 End Function 21 22 Public Shared Function ReadFromString(ByRef instance As IXmlSerializationAble, ByVal content As String) As Boolean 23 If content.Length = 0 Then 24 Return False 25 End If 26 Dim byteBuf() As Byte = System.Text.Encoding.Default.GetBytes(content) 27 Dim ms As New IO.MemoryStream(byteBuf) 28 29 Dim reader As New System.Xml.Serialization.XmlSerializer(instance.GetType, instance.GetIncludeTypes) 30 Try 31 instance = reader.Deserialize(ms) 32 Catch ex As Exception 33 Return False 34 Finally 35 ms.Close() 36 ms = Nothing 37 reader = Nothing 38 End Try 39 Return True 40 End Function 41 42 Public Shared Function Write(ByRef instance As IXmlSerializationAble, ByVal filename As String) As Boolean 43 Dim fi As IO.FileInfo 44 fi = New IO.FileInfo(filename) 45 Dim stream As IO.Stream = fi.Open(IO.FileMode.Create) 46 Dim writer As New System.Xml.Serialization.XmlSerializer(instance.GetType, instance.GetIncludeTypes) 47 Dim ns As New System.Xml.Serialization.XmlSerializerNamespaces 48 ns.Add("", "") 49 Try 50 '' 涓茶鍖栧璞?寮哄埗鍛藉悕绌洪棿涓虹┖ 51 writer.Serialize(stream, instance, ns) 52 Catch ex As Exception 53 Return False 54 Finally 55 stream.Close() 56 stream = Nothing 57 writer = Nothing 58 End Try 59 Return True 60 End Function

在XmlSerializationEx中,提供了Read、ReadFromString和Write几个静态方法。

让Xml配置文件读写变得轻松无比。

 

这里还提供了一个简单的方法,用于收集各种类型。

1 Public Shared Function GetArray(ByVal ParamArray types() As System.Type) As System.Type() 2 Return types 3 End Function

这是一个适合懒人使用的快速建立数组的方法,通过它,你无须Dim typeArr() as Type

下面写一个可以被用于串行化的类,作为例子。

1 Public Class XmlSerializationAbleSample 2 Implements IXmlSerializationAble 3 4 Public Field As ArrayList 5 6 Public Function GetIncludeTypes() As System.Type() Implements IXmlSerializationAble.GetIncludeTypes 7 Return GetArray(Field.GetType, GetType(Node)) 8 End Function 9 End Class

这里的Field.Gettype 将返回Arraylist类型的类型,GetType(Node)则返回包含在Field中的

并未显示的类的类型。

 

Ok,就到这里,我的AMindMap中也将使用这个方法来保存MindMap到文件,写在这里作为

读者的预备知识。

posted @ 2012-04-12 12:46  alan0405  阅读(1539)  评论(0编辑  收藏  举报