XmlSerializer是否会创建空文档

Gau*_*ier 2 c# xmlserializer

好的,下面的代码我已经生产了一年多没有变化.它一直运作良好.在过去一个月内,少数机器报告xml文档完全为空.它们甚至不包含xml标头.我无法复制突然变空的文件,也无法建议一种方法.我希望有人有类似的问题,他们解决了.

大多数使用此代码的机器已经使用了大约一年,如果不是更多的话.用于在其中包含数据和列表的空文件.文件不会同时序列化.在程序退出之前,一个接一个地保存/序列化.

我的问题:下面的代码是否有可能创建一个空文件?还有什么会导致他们突然变空?有没有其他人在过去一个月中遇到过XML-serializer的问题?(这个问题仅发生在过去一个月中已稳定3个月以上的版本.)

如果您有疑问或我遗失了什么请问.还有各种各样的类型,我序列化...所以如果你可以想象它我可能有一些类似的序列化.

 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Mal*_*olm 7

我们在$ WORK遇到过这个问题几次,症状是一个空文件,大小正确但填充零字节.

我们找到的解决方案是在FileStream上设置WriteThrough值:

using (Stream file = new FileStream(settingTemp, FileMode.Create,
                                   FileAccess.Write, FileShare.None,
                                   0x1000, FileOptions.WriteThrough))
{
   using (StreamWriter sw = new StreamWriter(file))
   {
      ...
   }
}
Run Code Online (Sandbox Code Playgroud)