Jeff Kirwan

Real World .NET

<November 2008>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456


Navigation

Blogs I Read

Subscriptions

News






The Joy of Serialization

I'm currently working on a Web Services project which passes back a whole slew of Custom Classes. I wanted to write a simple client app for testing, but did not want to have to populate a ton of text boxes, labels, etc. with the properties of the Custom Classes. I just need to see the XML representation of the Classes. Serialization to the rescue! I wrote this function which returns the XML of the serialized object as a string:

Private Function ObjToString(ByVal MyObject As Object) As String
     Dim MemStream As New IO.MemoryStream
     Dim ser As New Serialization.XmlSerializer(MyObject.GetType())
     ser.Serialize(MemStream, MyObject)
     MemStream.Position =
0
     Dim Reader As New IO.StreamReader(MemStream)
     Dim ReturnString As String
     ReturnString = Reader.ReadToEnd()
     MemStream.Close()
     Return ReturnString
End Function

 

Now I can pass any of the Custom Classes to a single Function, and get the XML back. It's hardly rocket science, but it works.

posted on Wednesday, September 24, 2003 10:36 AM by jkirwan





Powered by Dot Net Junkies, by Telligent Systems