将以往的XmlSerializer创建空文件 [英] Will XmlSerializer ever create empty documents
问题描述
好吧,下面的代码我已经在生产了一年多,没有变化。它一直合作得非常好。在过去的一个月内更多的则屈指可数机报告的XML文件是完全空。他们甚至不包含XML头。我不能复制的文件突然被空的,我也不能表明它发生的一种方法。我希望有人有过类似的问题,他们解决了。
大多数已经使用此代码的机器一直在使用它了约一年,如果不是更多。过去在them.The文件中的数据,并列出了空文件并不在同一时间序列。该保存/序列一前一后在程序退出之前
我的问题:
是否有可能下面的代码创建一个空文件?
还有什么会导致他们突然空的?
有任何人曾与XML的序列化问题在过去一个月? (此问题仅在过去的一个月发生在建立已稳定3+个月。)
如果您有任何疑问或我失去了一些东西问吧。此外,还有就是各种各样的,我序列化类型...因此,如果你能想象它,我可能也有类似的被序列化的东西。
公共类后端< T> {
公共字符串FileSaveLocation =这个被设置在启动时;
公共BOOL DisabledSerial;
公共虚拟无效BeforeDeserialize(){}
公共虚拟无效BeforeSerialize(){}
公共虚拟无效OnSuccessfulSerialize(){}
受保护的虚拟无效OnSuccessfulDeserialize(ListBackEnd< T> tmpList ){}
受保护的虚拟无效OnDeserialize(ListBackEnd< T> tmpList){}
公共虚拟无效连载()
{
如果(DisabledSerial)
返回;
试
{
BeforeSerialize();使用
(的TextWriter textWrite =新的StreamWriter(FileSaveLocation))
{
(新的XmlSerializer(this.GetType()))序列化(textWrite,这一点)。
的Debug.WriteLine([S]);
textWrite.Close();
}
OnSuccessfulSerialize();
}
赶上(例外五)
{
Static.Backup.XmlFile(FileSaveLocation);
Log.ErrorCatch(E,
XML,
连载 - + typeof运算(T)+ - +(新的FileInfo(FileSaveLocation))名称。);
}
}
公共虚拟对象的反序列化(TextReader的读者)
{
如果(this.DisabledSerial)
返回假;
ListBackEnd< T> TMP = NULL;
this.BeforeDeserialize();
如果(读者== NULL和放大器;&安培;!File.Exists(this.FileSaveLocation))
{
Log.Write(Family.Error,
XML,
反序列化 - + this.GetType()+ - 找不到文件+(新的FileInfo(FileSaveLocation))名称)。
}
,否则
{使用(TextReader的textRead =((阅读== NULL)
试
{
新的StreamReader(this.FileSaveLocation)?阅读器))
{
TMP =(ListBackEnd< T>)this.get_serializer()反序列化(textRead)。
的Debug.WriteLine([D]。);
textRead.Close();
}
OnSuccessfulDeserialize(TMP);
如果(TMP!= NULL)
{
this._Items = tmp._Items;
this.AutoIncrementSeed = tmp.AutoIncrementSeed;
如果(this._Items.Count> this.AutoIncrementSeed&放大器;&放大器; this._Items [0]是ItemStorage)
this.AutoIncrementSeed = this._Items.Max(项目= GT;(项目如ItemStorage)。重点);
}
}
赶上(例外五)
{
//这只是将文件复制
Static.Backup.XmlFile(FileSaveLocation);
//这仅记录异常
Log.ErrorCatch(E,
XML,
反序列化 - + typeof运算(T)+ - +(新的FileInfo (FileSaveLocation))名称)。
}
}
// {Log.ErrorCatch(即,XML,反序列化+ this.GetType()+ - + this.FileSaveLocation); }
OnDeserialize(TMP);
TMP = NULL;
返回(_Items.Count大于0);
}
}
的只有我可以认为这会发生的原因是,该行:
(新的XmlSerializer(this.GetType()))。序列化(textWrite,这一点);
抛出一个异常,并在创建的TextWriter和处置无任何写入它曾经拥有。我想看看你的日志中的错误。
这是什么
Static.Backup.XmlFile(FileSaveLocation);
吗?
Alright, The following code I've had in production for over a year with no changes. It has been working quite well. Within the past month more then a handful machines report that the xml documents are completely empty. They do not even contain a xml header . I cannot duplicate the files suddenly being empty, nor can i suggest a way for it to happen. I am hoping someone has had a similar issue that they solved.
Most of the machine that have been using this code have been using it for about a year, if not more. The empty files used to have data and lists in them.The files do not serialize at the same time. The save / serialize one after the other before the program exits.
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.)
If you have questions or i missing something ask please. There also is a wide variety of types that i serialize... so if you can imagine it I probably have something similar that gets serialized.
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);
}
}
The only reason I can think that this would happen is that this line:
(new XmlSerializer(this.GetType())).Serialize(textWrite, this);
throws an exception and the textwriter is created and disposed without ever having anything written to it. I'd look at your log for errors.
What does
Static.Backup.XmlFile(FileSaveLocation);
do?
这篇关于将以往的XmlSerializer创建空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!