Will XmlSerializer create empty documents

Ok, the following code that I used throughout the year is unchanged. It works quite well. Over the past month, more than a few computers have reported that the xml documents are completely empty. They do not even contain an xml header. I cannot duplicate files that suddenly become empty, and I cannot offer a way for this. I hope someone had a similar problem that they solved.

Most machines that have used this code use it for about a year, if not more. Empty files used to store data and lists in them. Files are not serialized at the same time. Saving / serializing one by one until the program is released.

My questions are: is it possible to create an empty file for the code below? What else could lead to their sudden emptiness? Anyone else had problems with the XML serializer last month? (This problem occurred only last month on lines that were stable for 3 months.)

If you have questions or ask something, I ask. There are also many types that I serialize ... so if you can imagine it, I probably have something similar that is serializing.

public class BackEnd<T> { public string FileSaveLocation = "this gets set on startup"; public bool DisabledSerial; public virtual void BeforeDeserialize() { } public virtual void BeforeSerialize() { } public virtual void OnSuccessfulSerialize() { } protected virtual void OnSuccessfulDeserialize(ListBackEnd<T> tmpList) { } protected virtual void OnDeserialize(ListBackEnd<T> tmpList) { } public virtual void serialize() { if (DisabledSerial) return; try { BeforeSerialize(); using (TextWriter textWrite = new StreamWriter(FileSaveLocation)) { (new XmlSerializer(this.GetType())).Serialize(textWrite, this); Debug.WriteLine(" [S]"); textWrite.Close(); } OnSuccessfulSerialize(); } catch (Exception e) { Static.Backup.XmlFile(FileSaveLocation); Log.ErrorCatch(e, "xml", "serialize - " + typeof(T) + " - " + (new FileInfo(FileSaveLocation)).Name); } } public virtual object deserialize(TextReader reader) { if (this.DisabledSerial) return false; ListBackEnd<T> tmp = null; this.BeforeDeserialize(); if (reader == null && !File.Exists(this.FileSaveLocation)) { Log.Write(Family.Error, "xml", "deserialize - " + this.GetType() + " - file not found. " + (new FileInfo(FileSaveLocation)).Name); } else { try { using (TextReader textRead = ((reader == null) ? new StreamReader(this.FileSaveLocation) : reader)) { tmp = (ListBackEnd<T>)this.get_serializer().Deserialize(textRead); Debug.WriteLine(" [D]"); textRead.Close(); } OnSuccessfulDeserialize(tmp); if (tmp != null) { this._Items = tmp._Items; this.AutoIncrementSeed = tmp.AutoIncrementSeed; if (this._Items.Count > this.AutoIncrementSeed && this._Items[0] is ItemStorage) this.AutoIncrementSeed = this._Items.Max(item => (item as ItemStorage).Key); } } catch (Exception e) { // this only copies the file Static.Backup.XmlFile(FileSaveLocation); // this only logs the exception Log.ErrorCatch(e, "xml", "deserialize - " + typeof(T) + " - " + (new FileInfo(FileSaveLocation)).Name); } } //{ Log.ErrorCatch(e, "xml", "deserialize" + this.GetType() + " - " + this.FileSaveLocation); } OnDeserialize(tmp); tmp = null; return (_Items.Count > 0); } } 
+2
source share
2 answers

The only reason I can think that this will happen is because this line:

 (new XmlSerializer(this.GetType())).Serialize(textWrite, this); 

throws an exception, and the text editor is created and deleted, without having anything written. I would look at your journal for errors.

What is he doing

 Static.Backup.XmlFile(FileSaveLocation); 

make?

+3
source

We encountered this problem several times in $ WORK, and the symptom was an empty file of the right size, but with zero bytes.

The solution we found is to set the WriteThrough value to FileStream:

 using (Stream file = new FileStream(settingTemp, FileMode.Create, FileAccess.Write, FileShare.None, 0x1000, FileOptions.WriteThrough)) { using (StreamWriter sw = new StreamWriter(file)) { ... } } 
+7
source

All Articles