facebook  linkedin  Twitter  skype  Rss googlePlus

Serialize dynamic objects to XML in VB.NET

Mar 13 2011
13393

 Essentially I have four methods here, one to serialize the objects and another to save it to a file, the others are to open the XML file and then deserialize it.

Private Sub SerializeArrayList(ByVal obj As ArrayList, ByVal FilePath As String)
            Dim doc As System.Xml.XmlDocument = New XmlDocument()
            Dim extraTypes As Type() = New Type(0) {}
            extraTypes(0) = GetType(CategoryInfo)
            Dim serializer As New System.Xml.Serialization.XmlSerializer(GetType(ArrayList), extraTypes)
            Dim stream As New System.IO.MemoryStream()
            Try
                serializer.Serialize(stream, obj)
                stream.Position = 0
                doc.Load(stream)
                '  Return doc.InnerXml
                'save doc to file.
                SaveTextToFile(doc.InnerXml, FilePath)
            Catch
                Throw
            Finally
                stream.Close()
                stream.Dispose()
            End Try
        End Sub
  
        Private Function DeSerializeArrayList(ByVal FilePath As String) As ArrayList
            'take file path, open file, drop to string.
            Dim serializedData As String = GetFileContents(FilePath)
            Dim list As New ArrayList()
            Dim extraTypes As Type() = New Type(0) {}
            extraTypes(0) = GetType(CategoryInfo)
            Dim serializer As New System.Xml.Serialization.XmlSerializer(GetType(ArrayList), extraTypes)
            Dim xReader As XmlReader = XmlReader.Create(New StringReader(serializedData))
            Try
                Dim obj As Object = serializer.Deserialize(xReader)
                list = DirectCast(obj, ArrayList)
            Catch
                Throw
            Finally
                xReader.Close()
            End Try
            Return list
        End Function
  
        Public Function GetFileContents(ByVal FullPath As String, Optional ByRef ErrInfo As String = "") As String
  
            Dim strContents As String
            Dim objReader As StreamReader
            Try
                objReader = New StreamReader(FullPath)
                strContents = objReader.ReadToEnd()
                objReader.Close()
                Return strContents
            Catch Ex As Exception
                ErrInfo = Ex.Message
                Return ""
            End Try
        End Function
  
        Public Function SaveTextToFile(ByVal strData As String, ByVal FullPath As String, Optional ByVal ErrInfo As String = "") As Boolean
  
            Dim bAns As Boolean = False
            Dim objReader As StreamWriter
            Try
                objReader = New StreamWriter(FullPath)
                objReader.Write(strData)
                objReader.Close()
                bAns = True
            Catch Ex As Exception
                ErrInfo = Ex.Message
            End Try
            Return bAns
        End Function

About the Author, Patrick Santry

Patrick Santry, has two decades of experience in enabling businesses to take advantage of the digital landscape. A well rounded experience in technology, and business is what sets me apart from the rest of the pack. When it comes to an overall digital strategy my experience is impressive.

BS in Computer Information Systems. Four time recipient of the Microsoft MVP Award, and author of several books and magazine articles on digital technologies.


blog comments powered by Disqus

 

Thank you for visiting our site, before you leave, please visit some of the areas or information you may have missed.

Popular Articles