|
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.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:
="1.0"="UTF-16"
<Sezioni>
<PLC>
<IP>10.0.0.85</IP>
<Port>502</Port>
<Description></Description>
<PauComm>15000</PauComm>
<ConTOut>5000</ConTOut>
<RxTOut>5000</RxTOut>
</PLC>
</Sezioni>
and this is the file after a save:
="1.0"="UTF-16"
<Sezioni>
<PLC>
<IP>10.0.0.85</IP>
<Port>502</Port>
<Description></Description>
<PauComm>15000</PauComm>
<ConTOut>5000</ConTOut>
<RxTOut>5000</RxTOut>
</PLC>
</Sezioni>
<Sezioni>
<PLC>
<IP>192.168.1.10</IP>
<Port>502</Port>
<Description />
<PauComm>15000</PauComm>
<ConTOut>5000</ConTOut>
<RxTOut>5000</RxTOut>
</PLC>
</Sezioni>
Obviously this is not the result I want to achieve, I do not want to duplicate fields, I want to update the existing ones.
Can someone tell me what I'm doing wrong?
Thanks in advance.
|
|
|
|
|
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.
|
|
|
|
|
...ops...
...next time I'll be more careful...
Thanks Eddy
|
|
|
|