Tuesday, June 26, 2012

Xml Serialization

Hi Geeks,
i am going to post a Generic Xml Serializer for visual basic



Imports System.IO

Imports System.Xml
Imports System.Text
Imports System.Xml.Serialization

Module Serializer
    Public Function SerializeObject(ByVal obj As [Object]) As String
        Dim memoryStream As New MemoryStream()
        Dim xs As New XmlSerializer(obj.[GetType]())
        Dim xmlTextWriter As New XmlTextWriter(memoryStream, Encoding.UTF8)
        xmlTextWriter.Formatting = Formatting.Indented
        xs.Serialize(xmlTextWriter, obj)
        memoryStream = DirectCast(xmlTextWriter.BaseStream, MemoryStream)
        Dim xml As String = UTF8ByteArrayToString(memoryStream.ToArray())
        xml = CleanEmptyTags(xml)
        Return xml
    End Function

    Public Function DeserializeObject(Of T)(ByVal objString As String) As T
        Dim obj As [Object] = Nothing
        Dim xs As New XmlSerializer(GetType(T))
        Using memoryStream As New MemoryStream(StringToUTF8ByteArray(objString))
            Dim xtr As New XmlTextReader(memoryStream)
            obj = xs.Deserialize(xtr)
        End Using
        Return DirectCast(obj, T)
    End Function

    Private Function UTF8ByteArrayToString(ByVal characters As Byte()) As String
        Dim encoding As New UTF8Encoding()
        Return encoding.GetString(characters)
    End Function

    Private 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 String
        Dim regex As New RegularExpressions.Regex("(\s)*<(\w)*(\s)*/>")
        Return regex.Replace(xml, String.Empty)
    End Function

End Module


and  you can use this like this


Deserialize  the XML string to object like this


Dim strxml As String = "<XMLSTRING/>"
Dim mtype As New ClassType
mtype = Serializer.DeserializeObject(Of ClassType)(strxml)

Serialize the XML string to object like this

Dim strxml As String = ""
Dim mtype As New ClassType
strxml = Serializer.serializeObject(mtype)
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 there

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 StrFont
    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
Now use it with a new property with type Strfont now strfont is easily serialized