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) {
source share