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

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » File Directory » Serialize ObjectScreenshots 
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.