Results 1 to 2 of 2

Thread: Saving Multiple Objects / Classes in one XML file

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2021
    Posts
    1

    Saving Multiple Objects / Classes in one XML file

    I've been away for programming for a while and it took me a bit to get used to XML serialization. My first issue was with multiple classes of different types into one file. I has to combine a couple answers together to get the hang of it, though it makes perfect sense now. I thought I would post this for anyone else who might want an example to help understand.

    For those who don't a lot of XML experience, proper XML has one root object. This object can be a List(of ...), so if you need to save multiple instances of a single data type or class you can use below...

    Code:
    Public Sub SaveMany()
            Dim TheList as New List(Of String)
            TheList.Add("Apple")
            TheList.Add("Banana")
            Dim mydocpath As String =
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            Dim writer As New System.Xml.Serialization.XmlSerializer(GetType(List(Of String))
            Dim file As New System.IO.StreamWriter(mydocpath + "\Test.xml")
            writer.Serialize(file, TheList)
            file.Close()
    End Sub
    If you have different data type or classes you will need to create a class that contains them. In the below I create a card class and pet class, put them in lists to save multiple of each them combine those in a single super class that can be output in one XML.

    Code:
    Public Class clsCard
        Private myRank As String
        Private mySuit As String
    
        Property Rank() As String
            Get
                Return myRank
            End Get
            Set(value As String)
                myRank = value
            End Set
        End Property
        Property Suit() As String
            Get
                Return mySuit
            End Get
            Set(value As String)
                mySuit = value
            End Set
        End Property
    End Class
    
    Public Class clsPet
        Private myPetName As String
        Private myPetType As String
        Private myAge As Int16
    
        Property PetName() As String
            Get
                Return myPetName
            End Get
            Set(value As String)
                myPetName = value
            End Set
        End Property
        Property PetType() As String
            Get
                Return myPetType
            End Get
            Set(value As String)
                myPetType = value
            End Set
        End Property
        Property Age As Int32
            Get
                Return myAge
            End Get
            Set(value As Int32)
                myAge = value
            End Set
        End Property
    End Class
    
    Public Class clsRootForXML
        Private myRecordDate As Date
        Private myCardList As List(Of clsCard)
        Private myPetList As List(Of clsPet)
        Property RecordDate As Date
            Get
                Return myRecordDate
            End Get
            Set(value As Date)
                myRecordDate = value
            End Set
        End Property
        Property CardList As List(Of clsCard)
            Get
                Return myCardList
            End Get
            Set(value As List(Of clsCard))
                myCardList = value
            End Set
        End Property
        Property PetList As List(Of clsPet)
            Get
                Return myPetList
            End Get
            Set(value As List(Of clsPet))
                myPetList = value
            End Set
        End Property
    End Class
    
    Public Sub CreateOneXML()
    
            Dim card1 As New clsCard
            Dim card2 As New clsCard
            Dim card3 As New clsCard
            Dim theseCards As New List(Of clsCard)
            card1.Rank = "Ace"
            card1.Suit = "Spades"
            card2.Rank = "King"
            card2.Suit = "Hearts"
            card3.Rank = "10"
            card3.Suit = "Diamonds"
            theseCards.Add(card1)
            theseCards.Add(card2)
            theseCards.Add(card3)
    
            Dim pet1 As New clsPet
            Dim pet2 As New clsPet
            Dim thesePets As New List(Of clsPet)
            pet1.PetName = "Spot"
            pet1.PetType = "Dog"
            pet1.Age = 3
            pet2.PetName = "Whiskers"
            pet2.PetType = "Cat"
            pet2.Age = 7
            thesePets.Add(pet1)
            thesePets.Add(pet2)
    
            Dim oneRootObject As New clsRootForXML
            oneRootObject.RecordDate = #01/01/2001#
            oneRootObject.CardList = theseCards
            oneRootObject.PetList = thesePets
    
            Dim mydocpath As String =
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            Dim writer As New System.Xml.Serialization.XmlSerializer(GetType(clsRootForXML))
            Dim file As New System.IO.StreamWriter(mydocpath + "\Test.xml")
            writer.Serialize(file, oneRootObject)
            file.Close()
        End Sub
    the resulting xml is
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <clsRootForXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <RecordDate>2001-01-01T00:00:00</RecordDate>
      <CardList>
        <clsCard>
          <Rank>Ace</Rank>
          <Suit>Spades</Suit>
        </clsCard>
        <clsCard>
          <Rank>King</Rank>
          <Suit>Hearts</Suit>
        </clsCard>
        <clsCard>
          <Rank>10</Rank>
          <Suit>Diamonds</Suit>
        </clsCard>
      </CardList>
      <PetList>
        <clsPet>
          <PetName>Spot</PetName>
          <PetType>Dog</PetType>
          <Age>3</Age>
        </clsPet>
        <clsPet>
          <PetName>Whiskers</PetName>
          <PetType>Cat</PetType>
          <Age>7</Age>
        </clsPet>
      </PetList>
    </clsRootForXML>
    Hope this helps.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Saving Multiple Objects / Classes in one XML file

    Thread moved to CodeBank, as it is a working snippet/example rather than a question.
    My usual boring signature: Nothing

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