Serialize to Xml : Serialize Object « File Directory « VB.Net






Serialize to Xml

   


Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
    Public Class MainClass
        Public Shared Sub Main()
            Dim roster = New EmployeeCollection(DateTime.Now)
            Dim employees = New Employee() _
                {New Employee With {.Id = 1, .Name = "A", _
                                    .Title = "Coder", _
                                    .HireDate = DateTime.Now, _
                                    .HourlyRate = 100.0}, _
                 New Employee With {.Id = 4, .Name = "B", _
                                    .Title = "Coder", _
                                    .HireDate = DateTime.Now, _
                                    .HourlyRate = 100.75}}

            roster.Employees = employees
            Dim serializer As New XmlSerializer(GetType(EmployeeCollection))
            Dim fs As New FileStream("EmployeeCollection.xml", FileMode.Create)

            serializer.Serialize(fs, roster)
            fs.Close()

            roster = Nothing
            fs = New FileStream("EmployeeCollection.xml", FileMode.Open)
            roster = DirectCast(serializer.Deserialize(fs), EmployeeCollection)
            serializer.Serialize(Console.Out, roster)
        End Sub

    End Class
    <XmlRoot("EmployeeCollection")> _
    Public Class EmployeeCollection
        <XmlElement(ElementName:="LastUpdated", datatype:="date")> _
        Public LastUpdated As DateTime
        <XmlArray("Employees"), XmlArrayItem("Employee")> _
        Public Employees As Employee()
        Public Sub New()
        End Sub
        Public Sub New(ByVal update As DateTime)

            Me.LastUpdated = update

        End Sub

    End Class
    Public Class Employee

        <XmlElement("Name")> _
        Public Name As String = String.Empty

        <XmlElement("Title")> _
        Public Title As String = String.Empty

        <XmlElement(ElementName:="HireDate", datatype:="date")> _
        Public HireDate As DateTime = Date.MinValue

        <XmlElement("HourlyRate")> _
        Public HourlyRate As Decimal = 0

        <XmlAttribute(AttributeName:="id", DataType:="integer")> _
        Public Id As String = String.Empty
        Public Sub New()
        End Sub
        Public Sub New(ByVal employeName As String, ByVal employeeTitle As String, ByVal employeeHireDate As DateTime, ByVal employeeHourlyRate As Decimal)

            Me.Name = employeName
            Me.Title = employeeTitle
            Me.HireDate = employeeHireDate
            Me.HourlyRate = employeeHourlyRate

        End Sub

    End Class

   
    
    
  








Related examples in the same category

1.Serialize Data to Binary and XML at the same time
2.Serialize Object to XML Stream and output to screen
3.Save Serializable Object to binary file: create a sequential-access file
4.Serializable Person Object
5.Simple Serializable Person Object
6.MarshalByRefObject access objects across application domain