Hi Geeks,
i am going to post a Generic Xml Serializer for visual basic
Imports System.IO
Imports System.XmlImports System.TextImports System.Xml.SerializationModule SerializerPublic Function SerializeObject(ByVal obj As [Object]) As StringDim memoryStream As New MemoryStream()Dim xs As New XmlSerializer(obj.[GetType]())Dim xmlTextWriter As New XmlTextWriter(memoryStream, Encoding.UTF8)xmlTextWriter.Formatting = Formatting.Indentedxs.Serialize(xmlTextWriter, obj)memoryStream = DirectCast(xmlTextWriter.BaseStream, MemoryStream)Dim xml As String = UTF8ByteArrayToString(memoryStream.ToArray())xml = CleanEmptyTags(xml)Return xmlEnd FunctionPublic Function DeserializeObject(Of T)(ByVal objString As String) As TDim obj As [Object] = NothingDim xs As New XmlSerializer(GetType(T))Using memoryStream As New MemoryStream(StringToUTF8ByteArray(objString))Dim xtr As New XmlTextReader(memoryStream)obj = xs.Deserialize(xtr)End UsingReturn DirectCast(obj, T)End FunctionPrivate Function UTF8ByteArrayToString(ByVal characters As Byte()) As StringDim encoding As New UTF8Encoding()Return encoding.GetString(characters)End FunctionPrivate Function StringToUTF8ByteArray(ByVal xmlString As String) As Byte()Dim encoding As New UTF8Encoding()Return encoding.GetBytes(xmlString)End Function''' <summary>''' //////////Deletes empty Xml tags from the passed xml''' </summary>''' <param name="xml"></param>''' <returns></returns>Public Function CleanEmptyTags(ByVal xml As String) As StringDim regex As New RegularExpressions.Regex("(\s)*<(\w)*(\s)*/>")Return regex.Replace(xml, String.Empty)End FunctionEnd Module
Deserialize the XML string to object like this
Serialize the XML string to object like this
Dim strxml As String = "<XMLSTRING/>"Dim mtype As New ClassTypemtype = Serializer.DeserializeObject(Of ClassType)(strxml)
so It seems the xml Serialization pretty much simple, but it's not like that, the hard part is you cannot serialize an Object if the class is not containing a non parameter constructor(default constructor) , for some classes there is no such constructors eg(Font class) to over come the situation we need a little trick thereDim strxml As String = ""Dim mtype As New ClassTypestrxml = Serializer.serializeObject(mtype)
Now take look at the font class all the constructor of the font class has parameters so when you use a Font as a property of a class and use the above method you have fall in a error
To avoid the error you need to use the xml ignore attribute to that property this will simply hide the font property to the serializer but the font is not saved to the xml if you want to save it you have to create a new property with the following consideration
Public Structure StrFontNow use it with a new property with type Strfont now strfont is easily serialized
Public FontFamily As String
Public GraphicsUnit As GraphicsUnit
Public Size As Single
Public Style As FontStyle
Sub New(ByVal F As Font)
FontFamily = F.FontFamily.Name
GraphicsUnit = F.Unit
Size = F.Size
Style = F.Style
End Sub
Public Function Tofont() As Font
Return New Font(FontFamily, Size, Style, GraphicsUnit)
End Function
End Structure