2010-12-07 9 views
2

申し訳ありませんが、変更なしで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); 
    } 
} 

答えて

3

私はこれが起こるだろうと考えることができる唯一の理由は、この行ということです:

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

が例外をスローしたTextWriterが今までそれに書かれた何もせずに作成して配置されています。私はあなたのログにエラーがないかどうかを調べます。

Static.Backup.XmlFile(FileSaveLocation); 

は何をしますか?

+0

:私たちが見つけ

ソリューションは、FileStreamを上のライトスルー値を設定することでした。 usingステートメント内の例外がそのようなファイルを空にすることは決して私にはありませんでした。他のものはうまくいっています。 ダスティンに感謝します。 – Gauthier

+0

助けてよかった;) –

+0

ファイルのコピーを作成するだけです。何らかの理由でオプションのフィールド属性を正しく追加しなかった場合は、元の内容が保存されます。 – Gauthier

7

この問題は、$ WORKで数回発生しましたが、症状は正しいサイズの空のファイルで、ゼロバイトで埋められています。私はそれを取るよ

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

あなたは聖人です、言及してくれてありがとうございます。 – jemidiah

+0

FYI:これは書き込みキャッシュを無効にし(Windowsで使用されるように)、ファイルに直接コミットします(まだ/ autoflushをフラッシュする必要がありますか?)。 0x1000は4096のデフォルトバッファサイズです。 – urbanhusky

+0

私は非常に同じ問題に直面しています。根本原因が何であるか知っていますか? –

関連する問題