Saving several strings to an XML file
Hello,
I am creating a programme, where I type a sentence into a textbox, then send to an XML file to be stored, which works fine.
However when I go to write a 'new' sentence in the textbox and save it to that xml file, and it overwrites the original sentence. I am not great at programming so sorry if I sound stupid sayign this, but I believe I am using what is called a serializable data class to save to my XML file.
If anybody could help me out I would be very grateful.
Thanks
Re: Saving several strings to an XML file
Sounds like you're not setting the correct line to write to, so you are always writing to the same position.
Post the code you are using.
Re: Saving several strings to an XML file
Thanks for your quick reply, this code below is my Serializable_Data Class
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml.XmlDocument
Imports System.Xml
Public Class Serializable_Data
'Save - serialize the object to disk ....
Public Function Save(ByVal filename As String)
'make a temporary filename ....
Dim tempfilename As String
tempfilename = filename & ".tmp"
'does the file exist?
Dim tempFileinfo As New FileInfo(tempfilename)
If tempFileinfo.Exists = True Then tempFileinfo.Delete()
'open file.....
Dim stream As New FileStream(tempfilename, FileMode.Create)
'save the object....
Save(stream)
'close the file ....
stream.Close()
'remove the existing data file and
'rename the temp file....
tempfileinfo.CopyTo(filename, True)
tempfileinfo.Delete()
End Function
'save - actually perform the serialisation ....
Public Function Save(ByVal stream As Stream)
'create serializer ...
Dim serializer As New XmlSerializer(Me.GetType)
'save the file....
serializer.Serialize(stream, Me)
End Function
'Load - deserialize from directory...
Public Shared Function Load(ByVal filename As String, _
ByVal newType As Type) As Object
'does file exist?...
Dim fileinfo As New FileInfo(filename)
If fileinfo.Exists = False Then
'create a blank version of the object and return that ...
Return System.Activator.CreateInstance(newType)
End If
'open the file...
Dim stream As New FileStream(filename, FileMode.Open)
'load the object from the stream...
Dim newObject As Object = Load(stream, newType)
'close the stream...
stream.Close()
'reutrn the object...
Return newObject
End Function
Public Shared Function Load(ByVal stream As Stream, _
ByVal newType As Type) As Object
'create serializer and load the object...
Dim serializer As New XmlSerializer(newType)
Dim newObject As Object = serializer.Deserialize(stream)
'return the new object...
Return newObject()
End Function
Then the following is on the actual form which performs the save:
Private Sub SendButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendButton.Click
'create a new incident object ...
Dim incident As New Incident()
'copy the values from the form into the incident....
PopulateIncidentFromForm(incident)
'save incident .....
Dim filename As String = DataFilename
incident.Save(filename)
'tell the user....
MsgBox("The incident has been logged")
End Sub
'DataFilename - where should we store our data?
Public ReadOnly Property DataFilename() As String
Get
'get our working folder.....
Dim folder As String
folder = Environment.CurrentDirectory
'return the folder with the name "classificationsystem.xml"....
Return folder & "\classificationsystem.xml"
End Get
End Property
'PopulateIncidentFromForm - populates Incident from the form fields.....
Public Sub PopulateIncidentFromForm(ByVal incident As Incident)
'copy the values....
incident.name = NameTextBox.Text
incident.department = DepartmentTextBox.Text
incident.incident = InputArea.Text
incident.classification = ClassTextBox.Text
End Sub
Re: Saving several strings to an XML file
Here is an example of reading an existing xml file which is displayed in a DataGridView (only for demo purposes). There are two buttons, one to add a new row and another button to save contents back to the xml file.
Once you study and understand the code you can apply it to your project by placing what is needed into your class and also where it is used i.e. in a form. If something does not make sense (most likely the LINQ statements) then there are plenty of helpful code samples here 101 Visual Basic LINQ Samples.
VB.NET VS2010 code
Code:
Public Class Form1
Private FileName As String = IO.Path.Combine(Application.StartupPath, "Incidents.xml")
Private Incidents As New List(Of Incident)
Private bsIncidents As New BindingSource
Private Sub cmdAddRow_Click(
ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cmdAddRow.Click
DirectCast(bsIncidents.DataSource, List(Of Incident)).Add(
New Incident With
{
.classification = "CC",
.department = "C1",
.incident = "Incident3",
.name = "Name3"
}
)
bsIncidents.ResetBindings(False)
End Sub
Private Sub cmdWriteConentsToDisk_Click(
ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cmdWriteConentsToDisk.Click
Dim Contents As XDocument = _
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Incidents>
<%= From T In Incidents Select _
<Incident>
<Classification><%= T.classification %></Classification>
<Department><%= T.department %></Department>
<IncidentID><%= T.incident %></IncidentID>
<Name><%= T.name %></Name>
</Incident> %>
</Incidents>
Contents.Save(FileName)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If IO.File.Exists(FileName) Then
Dim Document As New XDocument
Document = XDocument.Load(FileName)
Incidents = (From T In Document...<Incident>
Select New Incident With
{
.classification = T.<Classification>.Value,
.department = T.<Department>.Value,
.incident = T.<IncidentID>.Value,
.name = T.<Name>.Value
}
).ToList
bsIncidents.DataSource = Incidents
DataGridView1.DataSource = bsIncidents
End If
End Sub
End Class
Class Incident save to Incident.vb and add to your project.
Code:
Class Incident
Property department As String
Property classification As String
Property incident As String
Property name As String
Public Sub New()
End Sub
End Class
Contents of xml file to start with.
Code:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Incidents>
<Incident>
<Classification>AA</Classification>
<Department>A1</Department>
<IncidentID>Incident1</IncidentID>
<Name>Name1</Name>
</Incident>
<Incident>
<Classification>BB</Classification>
<Department>B1</Department>
<IncidentID>Incident2</IncidentID>
<Name>Name2</Name>
</Incident>
</Incidents>
With that said you have been given what is needed to fold into your project and not a plug into your project solution.
An alternate is to read and write data by readin xml data and writing xml data via a DataSet i.e. ReadXml and WriteXml or if you have a schema file use a DataTable with the same methods.