Monday, March 22, 2010

ObjectSerializationInXML

Imports System
Imports System.Windows.Forms
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO

Namespace ObjectSerializationInXML
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim emp As New Employee()
emp.ID = 101
emp.Name = "Steve"
emp.Salary = 6000
Dim tw As TextWriter = New StreamWriter("employee.xml")
Dim xs As New XmlSerializer(GetType(Employee))
xs.Serialize(tw, emp)
tw.Close()
webBrowser1.Url = New Uri(AppDomain.CurrentDomain.BaseDirectory & "//employee.xml")
End Sub

Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim fs As New FileStream("employee.xml", FileMode.Open)
Dim newXs As New XmlSerializer(GetType(Employee))
Dim emp1 As Employee = DirectCast(newXs.Deserialize(fs), Employee)
If emp1 IsNot Nothing Then
listBox1.Items.Add(emp1.ID)
listBox1.Items.Add(emp1.Name)
listBox1.Items.Add(emp1.Salary)
End If
fs.Close()
End Sub
End Class
_
Public Class Employee
Private empID As Integer
Private empName As String
Private empSalary As [Decimal]

_
Public Property ID() As Integer
Get
Return empID
End Get
Set(ByVal value As Integer)
empID = value
End Set
End Property
_
Public Property Name() As String
Get
Return empName
End Get
Set(ByVal value As String)
empName = value
End Set
End Property
_
Public Property Salary() As Decimal
Get
Return empSalary
End Get
Set(ByVal value As Decimal)
empSalary = value
End Set
End Property
End Class
End Namespace

No comments:

Post a Comment