Results 1 to 3 of 3

Thread: TOP URGENT - XML Serialisation.HELP

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    35

    TOP URGENT - XML Serialisation.HELP

    (VB.Net) I have a populated class. I want to serialise this class into a stream and then load it into an XML document.

    Due to other constraints I do not want to write the stream to a file on the hard drive. I need to store it in memory then load it into an XML dom document.

    Any examples anyone ?

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi

    Maybe you can use this?
    VB Code:
    1. Private st As IO.MemoryStream 'The stream to hold the data
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         'Serializes the class to a memorystream.
    5.         Dim mc As New MyClassToSerialize()
    6.         st = New IO.MemoryStream()
    7.  
    8.         mc.A = 1
    9.         mc.B = 2
    10.         mc.C = 3
    11.  
    12.         mc.Save(st)
    13.  
    14.     End Sub
    15.  
    16.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    17.         'De-serializes the class from the memorystream
    18.         Dim mc As MyClassToSerialize
    19.         mc = MyClassToSerialize.Load(st)
    20.         MsgBox(mc.A)
    21.         MsgBox(mc.B)
    22.         MsgBox(mc.C)
    23.     End Sub
    24.  
    25.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    26.         'Loads the serialized class as XML text.
    27.         Dim S As String
    28.         S = MyClassToSerialize.LoadToXML(st)
    29.         MsgBox(S)
    30.     End Sub
    31.  
    32. 'The class to serialize.
    33. <Serializable()> Public Class MyClassToSerialize
    34.     Public A As Integer
    35.     Public B As Integer
    36.     Public C As Integer
    37.  
    38.     Public Sub Save(ByVal st As IO.Stream)
    39.         Dim xml As New Xml.Serialization.XmlSerializer(GetType(MyClassToSerialize))
    40.         st.Position = 0
    41.  
    42.         xml.Serialize(st, Me)
    43.     End Sub
    44.  
    45.     Public Shared Function Load(ByVal st As IO.Stream) As MyClassToSerialize
    46.         Dim xml As New Xml.Serialization.XmlSerializer(GetType(MyClassToSerialize))
    47.         Dim mcts As MyClassToSerialize
    48.         st.Position = 0
    49.  
    50.         mcts = DirectCast(xml.Deserialize(st), MyClassToSerialize)
    51.         Return mcts
    52.     End Function
    53.     Public Shared Function LoadToXML(ByVal st As IO.Stream) As String
    54.         Dim xml As New Xml.XmlDocument()
    55.         st.Position = 0
    56.         xml.Load(st)
    57.         Return xml.OuterXml
    58.     End Function
    59. End Class
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    35
    Thanks very much for that. Spot on. Works a treat.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width