English 中文(简体)
Will XmlSerializer们永远不会制造空文件
原标题:Will XmlSerializer ever create empty documents

Alright, The following Code I ve have in production for over a year with no change. 它的工作进展良好。 在过去一个月里,一辆手提式机器报告说,Xml文件是完全空的。 他们甚至没有Xml头盔。 我不能突然重复这些档案,也不能提出这样做的办法。 我希望有人已经解决了类似的问题。

使用该代码的大部分机器已经使用了约一年,如果不是更多。 空档中的数据和清单是用的。 这些档案并不同时序列。 在方案退出之前,储蓄/序列。

My questions: is it possible for the code below to create an empty file? What else would cause them to be suddenly empty? Has anyone else had issues with XML-serializer in the past month? (This problem has only happened in the past month on builds that have been stable for 3+ months.)

如果你有问题或没有问题,请询问。 还有一些种类繁多的编号......这样,如果你能够想象,我可能具有类似的编号。

 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);
    }
}
最佳回答

我认为,这样做的唯一原因是:

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

案文撰写人没有提交任何书面材料就成立和处理。 我看一看你的错误。

什么

Static.Backup.XmlFile(FileSaveLocation);

问题回答

我们曾几次在WORK碰到这一问题,症状是真实规模的空洞文件,但以零件填充。

我们找到的解决办法是,在文件Stream上确定书写价值:

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签