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.