Will XmlSerializer ever create empty documents

后端 未结 2 588
不知归路
不知归路 2021-01-22 14:00

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 th

相关标签:
2条回答
  • 2021-01-22 14:32

    We've encountered this problem several times at $WORK, with the symptom being an empty file of the correct size but filled with zero bytes.

    The solution we found was to set the WriteThrough value on the FileStream:

    using (Stream file = new FileStream(settingTemp, FileMode.Create,
                                       FileAccess.Write, FileShare.None,
                                       0x1000, FileOptions.WriteThrough))
    {
       using (StreamWriter sw = new StreamWriter(file))
       {
          ...
       }
    }
    
    0 讨论(0)
  • 2021-01-22 14:34

    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?

    0 讨论(0)
提交回复
热议问题