Friday, 25 January 2013

XML Serialization in VB.NET 4.5

 XML Serialization in VB.NET 4.5

Step 1) Add a class Called (public) Person

   <Serializable> _
   Public Class Person
        Public Property ID() As Integer
        End Property
        Public Property firstname() As String
        End Property
        Public Property lastname() As String
        End Property

   End Class

Step 2)  Add a method to Serialize Person(s) object to XML


         Private  Sub XmlSearilize()
            Try
                Person p = New Person
                {
                     ID = 1, firstname = "syam", lastname = "benegal"
                }


                Dim ser As System.Xml.Serialization.XmlSerializer =  New XmlSerializer(Type.GetType(Person))
                Dim fs As FileStream =  New FileStream("c:\xml1.xml",FileMode.Create)
                ser.Serialize(fs, p)
                fs.Close()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try

         End Sub
 
Step 3)  Call this Method in Program Entry Point

        Public Shared  Sub Main()
            Dim xd As xmlse =  New xmlse()
            xd.XmlSearilize()
        End Sub

Step 4) Run the Application

open the file in C:\xml1.xml

<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ID>1</ID>
  <firstname>syam</firstname>
  <lastname>benegal</lastname>
</Person>

Example 2


XML Serialize for Array(s)( of Objects)

//Serialize Array of Objects

Private Sub XmlSearilize()
    Try
        Dim persons As Person() = New Person() {New Person() With { _
            .ID = 1, _
            .firstname = "syam", _
            .lastname = "benegal" _
        }, New Person() With { _
            .ID = 2, _
            .firstname = "kate", _
            .lastname = "winslet" _
        }, New Person() With { _
            .ID = 3, _
            .firstname = "keter", _
            .lastname = "paul" _
        }, New Person() With { _
            .ID = 4, _
            .firstname = "cat", _
            .lastname = "bengal" _
        }}
        Dim ser As System.Xml.Serialization.XmlSerializer = New XmlSerializer(GetType(Person()))
        Dim fs As New FileStream("c:\xml1.xml", FileMode.Create)
        ser.Serialize(fs, persons)
        fs.Close()
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try

End Sub

output:


<?xml version="1.0"?>
<ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Person>
    <ID>1</ID>
    <firstname>kate</firstname>
    <lastname>winslet</lastname>
  </Person>
  <Person>
    <ID>2</ID>
    <firstname>jenny</firstname>
    <lastname>mathew</lastname>
  </Person>
  <Person>
    <ID>3</ID>
    <firstname>keter</firstname>
    <lastname>paul</lastname>
  </Person>
  <Person>
    <ID>4</ID>
    <firstname>cat</firstname>
    <lastname>bengal</lastname>
  </Person>
</ArrayOfPerson>