S
S
SoftEngineer2017-05-14 08:38:00
C++ / C#
SoftEngineer, 2017-05-14 08:38:00

XmlSerializer in c# duplicates last 2-3 characters when writing to file?

Hello everybody.
I use the following code in c# to serialize an object to xml and write it to a file:

XmlSerializer formatter = new XmlSerializer(typeof(Config));
using (FileStream fs = new FileStream(File, FileMode.OpenOrCreate))
{
      formatter.Serialize(fs, Current);
}

But sometimes (which is the strangest thing) several characters are randomly duplicated at the end of the file
<?xml version="1.0"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ActivityTime>
    <FromTimeString>PT9H</FromTimeString>
    <ToTimeString>PT23H</ToTimeString>
  </ActivityTime>
  <ThreadsCount>2</ThreadsCount>
</Config>ig>>

What is this nonsense and how to deal with it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SoftEngineer, 2017-05-14
@SoftEngineer

solved the problem, instead of FileMode.OpenOrCreate you need to use FileMode.Create

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question