Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. XML Serialization

XML Serialization

Scheduled Pinned Locked Moved Visual Basic
xmljsonhelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrColeyted
    wrote on last edited by
    #1

    Hello all. I am trying to write a simple program that defines a Car class and writes an XML Serialized Car object to an XML file. This is a Console app. Right now, I have the XMLSerializer Object defined and initialized with Module Scope. Within the main method, I create a filestream and a streamwriter that I pass to it to write the serialized Car object to the file. This works fine, but when I re-open the program after closing it, it cannot read the object back in once more than one have been written to the .xml file. I assume that this is because when a second object is written to the file, the xml header is re-inserted into the same file, and it is throwing the reader off. I have posted me code below. I am trying to learn how to read and write XML with VB. Any help would be appreciated. Imports System.Xml.Serialization Imports System.IO Public Module Module1       Public xs As New Xml.Serialization.XmlSerializer(GetType(Car))       Dim car1 As Car       Dim car2 As Car       <Serializable> Public Class Car             Public Make As String             Public Model As String             Public Color As String             Private year As Integer             Public Property TheYear() As Integer                   Get                         Return year                   End Get                   Set(ByVal value As Integer)                         year = value                   End Set             End Property             Public Sub New()                   Make = "Chevy"                   Model = "Camero"                   Color = "Tan"

    N 1 Reply Last reply
    0
    • M MrColeyted

      Hello all. I am trying to write a simple program that defines a Car class and writes an XML Serialized Car object to an XML file. This is a Console app. Right now, I have the XMLSerializer Object defined and initialized with Module Scope. Within the main method, I create a filestream and a streamwriter that I pass to it to write the serialized Car object to the file. This works fine, but when I re-open the program after closing it, it cannot read the object back in once more than one have been written to the .xml file. I assume that this is because when a second object is written to the file, the xml header is re-inserted into the same file, and it is throwing the reader off. I have posted me code below. I am trying to learn how to read and write XML with VB. Any help would be appreciated. Imports System.Xml.Serialization Imports System.IO Public Module Module1       Public xs As New Xml.Serialization.XmlSerializer(GetType(Car))       Dim car1 As Car       Dim car2 As Car       <Serializable> Public Class Car             Public Make As String             Public Model As String             Public Color As String             Private year As Integer             Public Property TheYear() As Integer                   Get                         Return year                   End Get                   Set(ByVal value As Integer)                         year = value                   End Set             End Property             Public Sub New()                   Make = "Chevy"                   Model = "Camero"                   Color = "Tan"

      N Offline
      N Offline
      nlarson11
      wrote on last edited by
      #2

      change the filemode to 'create'

      Dim f As New FileStream(fileName, FileMode.Create, FileAccess.Write)

      or add this line before craete the file stream

      If IO.File.Exists(fileName) Then IO.File.Delete(fileName)

      'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

      M 1 Reply Last reply
      0
      • N nlarson11

        change the filemode to 'create'

        Dim f As New FileStream(fileName, FileMode.Create, FileAccess.Write)

        or add this line before craete the file stream

        If IO.File.Exists(fileName) Then IO.File.Delete(fileName)

        'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

        M Offline
        M Offline
        MrColeyted
        wrote on last edited by
        #3

        That would work if I were trying to only store data from one running of the app. However, I am really trying to be able to make the XML file grow through each subsequent running of the app. I could perhaps use your idea of deleting the file each time, if I could figure out how to get each object in the file into a collection on form_load so that they could be written back to the file when the app is closing. I am having a hard time getting my code to work with multiple objects. Is there a way to do this? Thanks. Btw, the code from my original posting is not complete.

        "If you don't know where you're going, you'll probably end up somewhere else." Yogi Berra

        N 1 Reply Last reply
        0
        • M MrColeyted

          That would work if I were trying to only store data from one running of the app. However, I am really trying to be able to make the XML file grow through each subsequent running of the app. I could perhaps use your idea of deleting the file each time, if I could figure out how to get each object in the file into a collection on form_load so that they could be written back to the file when the app is closing. I am having a hard time getting my code to work with multiple objects. Is there a way to do this? Thanks. Btw, the code from my original posting is not complete.

          "If you don't know where you're going, you'll probably end up somewhere else." Yogi Berra

          N Offline
          N Offline
          nlarson11
          wrote on last edited by
          #4

          forgive...my misunderstanding. in order to do what you want, upon every save of the serializer...add a delimiting row (like a row of asterisks). then your read part would read until it finds the delimiting row and then deserialize that chunk. read the next chunk and deserialize that...etc. <?xml version="1.0" encoding="utf-8"?> <Car xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Make>Chevy</Make> <Model>Camero</Model> <Color>Tan</Color> <TheYear>2003</TheYear> </Car> *************************************************************** <?xml version="1.0" encoding="utf-8"?> <Car xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Make>Chevy</Make> <Model>Camero</Model> <Color>Tan</Color> <TheYear>2003</TheYear> </Car>

          'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups