XDocument.Save()时的内存异常

Job*_*Joy 3 c# linq wpf exception-handling

我试图将XDcoument保存到没有足够可用内存空间的拇指驱动器.(这是应用程序的一个特殊测试条件)虽然应用程序给出了如下的异常,但我无法在XDocument.Save(filePath)周围的try catch块中得到它.看起来这是一个延迟投掷.这是LINQ问题还是我做错了什么?

替代文字http://img211.imageshack.us/img211/8324/exce.png

 System.IO.IOException was unhandled
 Message="There is not enough space on the disk.\r\n"
 Source="mscorlib"
 StackTrace:
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.FileStream.FlushWrite(Boolean calledFromFinalizer)
   at System.IO.FileStream.Dispose(Boolean disposing)
   at System.IO.FileStream.Finalize()
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 5

您在框架中发现了一个错误.XDocument.Save(string)使用"using"语句来确保输出流被释放.它取决于您在处理指令中使用的编码,但内部System.Xml.XmlUtf8RawTextReader将是实现文本编写器的常见编码.

错误:编写该类的Microsoft程序员忘记实现Dispose()方法.只实现了Close()方法.

这个错误尚未在connect.microsoft.com反馈网站上报告,这很奇怪.它应该在一般使用中引起麻烦,因为文件在终结器线程运行之前保持打开状态.虽然这通常不需要那么长,几秒钟左右.除非您在写入之后立即退出程序,并且在运行磁盘空间不幸的情况下,缓冲区将被刷新.

此错误的解决方法是使用XDocument.Save(TextWriter)重载,传递StreamWriter,其编码与XML的编码匹配.