Help Deserialize XML - Very New
Alright, first off, I'm very new to this whole Serialize / Deserialize.
I'm trying to Deserialize a piece of XML that I serialized. Here's all my code:
Writing to it.
Code:
Dim xmlSer As New XmlSerializer(GetType(List(Of ClassInformation)))
Dim ClassesList As New List(Of ClassInformation)
ClassesList.Add(New ClassInformation(.txtClassName.Text, _
.txtClassCode.Text, _
.txtClassRoom.Text, _
.numGrade.Value))
Dim sw As New StreamWriter(Application.StartupPath & "\Data\" & .txtClassCode.Text & ".xml", True)
xmlSer.Serialize(sw, ClassesList)
sw.Close()
Declaring my Class
Code:
Public Class ClassInformation
Public ClassName As String
Public ClassCode As String
Public ClassRoom As String
Public Grade As String
Public Sub New()
End Sub
Public Sub New(ByVal nClassName As String, ByVal nClassCode As String, ByVal nClassRoom As String, ByVal nGrade As String)
ClassName = nClassName
ClassCode = nClassCode
ClassRoom = nClassRoom
Grade = nGrade
End Sub
End Class
Reading
Code:
Public Sub ReadClass()
With frmClassCreate
Dim xmlDe As New XmlSerializer(GetType(ClassInformation))
Dim sr As New StreamReader(Application.StartupPath & "\Data\1.xml", True)
Dim sName As New ClassInformation
If sr.EndOfStream <> True Then
sName = xmlDe.Deserialize(sr)
MsgBox(sName.ClassName)
End If
sr.Close()
End With
End Sub
Re: Help Deserialize XML - Very New
two things.... 1) Most important... you didn't declare you class as serializable ... add <Serializable> right in front of your public class declaration... 2) I don't think fields can be serialized... try making them properties instead.
-tg
Re: Help Deserialize XML - Very New
Alright, I've been fiddling around with it, and it seems to work.
BUT, I want this to make it more appealing to the eye and organized.
vb Code:
<?xml version="1.0" encoding="utf-8"?>
<Classes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ClassesInfo> 'THESE THINGS <
<ClassName>1</ClassName>
<ClassCode>1</ClassCode>
<ClassRoom>1</ClassRoom>
<Grade>9</Grade>
</ClassesInfo> 'THESE THINGS <
<ClassesInfo> 'THESE THINGS <
<ClassName>5</ClassName>
<ClassCode>3</ClassCode>
<ClassRoom>2</ClassRoom>
<Grade>1</Grade>
</ClassesInfo> 'THESE THINGS <
</Classes>
So how would I do that? Here's my current code:
vb Code:
<XmlRoot(ElementName:="Classes")> _
<Serializable()> _
Public Class ClassInformation
Private oClassName As String
Private oClassCode As String
Private oClassRoom As String
Private oGrade As String
Public Property ClassName() As String
Get
Return oClassName
End Get
Set(ByVal value As String)
oClassName = value
End Set
End Property
Public Property ClassCode() As String
Get
Return oClassCode
End Get
Set(ByVal value As String)
oClassCode = value
End Set
End Property
Public Property ClassRoom() As String
Get
Return oClassRoom
End Get
Set(ByVal value As String)
oClassRoom = value
End Set
End Property
Public Property Grade() As String
Get
Return oGrade
End Get
Set(ByVal value As String)
oGrade = value
End Set
End Property
End Class
vb Code:
Dim xmlSer As New XmlSerializer(GetType(ClassInformation))
Dim dInfo As New ClassInformation
dInfo.ClassName = .txtClassName.Text
dInfo.ClassCode = .txtClassCode.Text
dInfo.ClassRoom = .txtClassRoom.Text
dInfo.Grade = .numGrade.Value
MsgBox(dInfo.ClassName & " " & dInfo.ClassRoom & " " & dInfo.ClassCode)
Dim sw As New StreamWriter(Application.StartupPath & "\Data\" & .txtClassCode.Text & ".xml", True)
xmlSer.Serialize(sw, dInfo)
sw.Dispose()
sw.Close()
Re: Help Deserialize XML - Very New
Re: Help Deserialize XML - Very New
Quote:
I want this to make it more appealing to the eye and organized. //snip// So how would I do that?
Depends on what you mean. Do you want your stuff in attributes rather than elements? Look up the XMLAttribute tag on MSDN ... that should do it.
-tg