XML Serialization
-
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"
-
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"
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
-
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
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
-
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
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