XML Serialization : XmlSerializer « XML « VB.Net Tutorial






Imports System.IO
Imports System.Xml.Serialization
Public Class Point
  Public X As Integer
  Public Y As Integer

  Public Overrides Function ToString() As String
    Return "(" & X & "," & Y & ")"
  End Function 
End Class

Module Test
  Sub Main()
    Dim P1 As New Point()
    P1.X = 6
    P1.Y = 4
    SaveToFile(P1)

    Dim P2 As Point = RetrieveFromFile()
    Console.WriteLine(P2.ToString())
  End Sub

  Sub SaveToFile(P as Point)
    Dim Serializer As New XmlSerializer(GetType(Point))
    Dim Writer As StreamWriter = New StreamWriter("point.xml") 
    Serializer.Serialize(Writer, P) 
    Writer.Close()
  End Sub

  Function RetrieveFromFile() As Point
    Dim ReturnObject As Point
    Dim Serializer As New XmlSerializer(GetType(Point)) 
    Dim FS As FileStream = New FileStream("point.xml", FileMode.Open) 

    ReturnObject = CType(Serializer.Deserialize(FS), Point) 
    FS.Close()
    Return ReturnObject
  End Function
End Module
(6,4)








25.7.XmlSerializer
25.7.1.Serialize Class to xml file
25.7.2.DeSerialize from xml file
25.7.3.XML Serialization