申し訳ありませんが、変更なしで1年以上実稼働していた次のコードです。それはかなりうまくいっています。過去1ヶ月以内には、XML文書が完全に空であると報告されました。彼らはxmlヘッダを含んでいません。私は突然空であるファイルを複製することはできませんし、それが起こる方法を提案することもできません。私は誰かが解決した同様の問題を抱えていることを期待しています。XmlSerializerは空のドキュメントを作成しますか
このコードを使用しているマシンのほとんどは、それ以上ではないにしても、約1年間使用しています。空のファイルは、データとリストを持っていました。ファイルは、同時にシリアル化されません。プログラムが終了する前に次々と保存/シリアル化します。
私の質問: 空のファイルを作成するためのコードはありますか? 他に何が突然空になるのでしょうか? 先月に他の誰かがXMLシリアライザに問題があったのですか? (この問題は先月、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);
}
}
:私たちが見つけ
ソリューションは、FileStreamを上のライトスルー値を設定することでした。 usingステートメント内の例外がそのようなファイルを空にすることは決して私にはありませんでした。他のものはうまくいっています。 ダスティンに感謝します。 – Gauthier
助けてよかった;) –
ファイルのコピーを作成するだけです。何らかの理由でオプションのフィールド属性を正しく追加しなかった場合は、元の内容が保存されます。 – Gauthier