I have wrote that code:
public void Save()
{
using (FileStream fs = new FileStream(Properties.Settings.Default.settings_file_path, FileMode.Open))
{
XmlSerializer ser = new XmlSerializer(typeof(MySettings));
ser.Serialize(fs, this);
}
}
When I am using FileMode.Open
everything is good, and output is e.x. like this:
<?xml version="1.0"?>
<MySettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<settingsList>
<Setting>
<Value>12</Value>
<Name>A0</Name>
<Type>MEASUREMENT</Type>
</Setting>
<Setting>
<Value>5000</Value>
<Name>C0</Name>
<Type>MEASUREMENT</Type>
</Setting>
</settingsList>
</MySettings>
but when I change it to FileMode.OpenOrCreate
output will change to:
<?xml version="1.0"?>
<MySettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<settingsList>
<Setting>
<Value>12</Value>
<Name>A0</Name>
<Type>MEASUREMENT</Type>
</Setting>
<Setting>
<Value>5000</Value>
<Name>C0</Name>
<Type>MEASUREMENT</Type>
</Setting>
</settingsList>
</MySettings>>
what makes whole xml file corrupted because of additional >
sign at the end.
Is this explanable or its c# bug?
See Question&Answers more detail:os