Results 1 to 5 of 5

Thread: Help Deserialize XML - Very New

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    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:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <Classes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    3. <ClassesInfo> 'THESE THINGS <
    4.   <ClassName>1</ClassName>
    5.   <ClassCode>1</ClassCode>
    6.   <ClassRoom>1</ClassRoom>
    7.   <Grade>9</Grade>
    8. </ClassesInfo> 'THESE THINGS <
    9. <ClassesInfo> 'THESE THINGS <
    10.   <ClassName>5</ClassName>
    11.   <ClassCode>3</ClassCode>
    12.   <ClassRoom>2</ClassRoom>
    13.   <Grade>1</Grade>
    14. </ClassesInfo> 'THESE THINGS <
    15. </Classes>

    So how would I do that? Here's my current code:

    vb Code:
    1. <XmlRoot(ElementName:="Classes")> _
    2. <Serializable()> _
    3. Public Class ClassInformation
    4.     Private oClassName As String
    5.     Private oClassCode As String
    6.     Private oClassRoom As String
    7.     Private oGrade As String
    8.  
    9.     Public Property ClassName() As String
    10.         Get
    11.             Return oClassName
    12.         End Get
    13.         Set(ByVal value As String)
    14.             oClassName = value
    15.         End Set
    16.     End Property
    17.  
    18.     Public Property ClassCode() As String
    19.         Get
    20.             Return oClassCode
    21.         End Get
    22.         Set(ByVal value As String)
    23.             oClassCode = value
    24.         End Set
    25.     End Property
    26.  
    27.     Public Property ClassRoom() As String
    28.         Get
    29.             Return oClassRoom
    30.         End Get
    31.         Set(ByVal value As String)
    32.             oClassRoom = value
    33.         End Set
    34.     End Property
    35.  
    36.     Public Property Grade() As String
    37.         Get
    38.             Return oGrade
    39.         End Get
    40.         Set(ByVal value As String)
    41.             oGrade = value
    42.         End Set
    43.     End Property
    44. End Class

    vb Code:
    1. Dim xmlSer As New XmlSerializer(GetType(ClassInformation))
    2.                 Dim dInfo As New ClassInformation
    3.                 dInfo.ClassName = .txtClassName.Text
    4.                 dInfo.ClassCode = .txtClassCode.Text
    5.                 dInfo.ClassRoom = .txtClassRoom.Text
    6.                 dInfo.Grade = .numGrade.Value
    7.                 MsgBox(dInfo.ClassName & " " & dInfo.ClassRoom & " " & dInfo.ClassCode)
    8.                 Dim sw As New StreamWriter(Application.StartupPath & "\Data\" & .txtClassCode.Text & ".xml", True)
    9.                 xmlSer.Serialize(sw, dInfo)
    10.                 sw.Dispose()
    11.                 sw.Close()
    Last edited by Wesley008; Apr 1st, 2009 at 07:43 PM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Help Deserialize XML - Very New

    Deleted - Look up 1
    Last edited by Wesley008; Apr 1st, 2009 at 07:43 PM.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Help Deserialize XML - Very New

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width