[VB.NET 2008] DataSet and XML files
-
Hi, in my application (running in Windows CE) I use a XML file to store some configuration data that, at runtime, I load in a DataSet. To read and to store these data I use the following code:
Private Shared Function SetCfgData() As Boolean
Try Dim fs As New FileStream(CGlobali.CfgDataFileComplName, FileMode.Open) Dim xr As XmlReader = System.Xml.XmlReader.Create(fs) 'CGlobali.CfgData is a DataSet CGlobali.CfgData.Clear() CGlobali.CfgData.ReadXml(xr) xr.Close() fs.Close() Catch ex As System.IO.FileNotFoundException MessageBox.Show("File " & CGlobali.CfgDataFileComplName & " not found.") Return False Catch ex As XmlException MessageBox.Show("XmlException occured: " & ex.ToString) Return False End Try Return True
When the user modifies these data, they are automatically updated in the DataSet and saved in the file, at least that's what I would do... To save the data from the DataSet to the file I do this:
Private Shared Function UpdateCgfFile() As Boolean
Try Dim StreamEncoding As Encoding StreamEncoding = Encoding.Unicode Dim cfgfile As New System.IO.StreamWriter(CGlobali.CfgDataFileComplName, True, StreamEncoding) CGlobali.CfgData.WriteXml(cfgfile) cfgfile.Close() Return True Catch ex As Exception Return False End Try
End Function
What I would get with the above code is to update existing fields in the xml file with the new values, but what I get is that the same fields are added to the existing ones. This is the "original" file:
10.0.0.85 502 15000 5000 5000
and this is the file after a save:
10.0.0.85 502 15000 5000 5000 192.168.1.10 502 15000 5000 5000
Obviously this is not the result I want to achieve, I do not want to du
-
Hi, in my application (running in Windows CE) I use a XML file to store some configuration data that, at runtime, I load in a DataSet. To read and to store these data I use the following code:
Private Shared Function SetCfgData() As Boolean
Try Dim fs As New FileStream(CGlobali.CfgDataFileComplName, FileMode.Open) Dim xr As XmlReader = System.Xml.XmlReader.Create(fs) 'CGlobali.CfgData is a DataSet CGlobali.CfgData.Clear() CGlobali.CfgData.ReadXml(xr) xr.Close() fs.Close() Catch ex As System.IO.FileNotFoundException MessageBox.Show("File " & CGlobali.CfgDataFileComplName & " not found.") Return False Catch ex As XmlException MessageBox.Show("XmlException occured: " & ex.ToString) Return False End Try Return True
When the user modifies these data, they are automatically updated in the DataSet and saved in the file, at least that's what I would do... To save the data from the DataSet to the file I do this:
Private Shared Function UpdateCgfFile() As Boolean
Try Dim StreamEncoding As Encoding StreamEncoding = Encoding.Unicode Dim cfgfile As New System.IO.StreamWriter(CGlobali.CfgDataFileComplName, True, StreamEncoding) CGlobali.CfgData.WriteXml(cfgfile) cfgfile.Close() Return True Catch ex As Exception Return False End Try
End Function
What I would get with the above code is to update existing fields in the xml file with the new values, but what I get is that the same fields are added to the existing ones. This is the "original" file:
10.0.0.85 502 15000 5000 5000
and this is the file after a save:
10.0.0.85 502 15000 5000 5000 192.168.1.10 502 15000 5000 5000
Obviously this is not the result I want to achieve, I do not want to du
steve_9496613 wrote:
Can someone tell me what I'm doing wrong?
Dim cfgfile As New System.IO.StreamWriter(CGlobali.CfgDataFileComplName, True, StreamEncoding)
What does the second parameter in the
StreamWriter
do?true to append data to the file; false to overwrite the file. If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]
-
steve_9496613 wrote:
Can someone tell me what I'm doing wrong?
Dim cfgfile As New System.IO.StreamWriter(CGlobali.CfgDataFileComplName, True, StreamEncoding)
What does the second parameter in the
StreamWriter
do?true to append data to the file; false to overwrite the file. If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]
...ops... :sigh: ...next time I'll be more careful... Thanks Eddy
-
...ops... :sigh: ...next time I'll be more careful... Thanks Eddy