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.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
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
6.MarshalByRefObject access objects across application domain
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.