XmlDictionaryWriter 在处理时关闭输出流,.NET Portable
XmlDictionaryWriter closes the output stream when disposed, .NET Portable
我的上下文是 .NET PCL Profile111。
我尝试使用 DataContractSerializer
和 XmlDictionaryWriter
进行二进制 xml 序列化。我遇到的问题是,在我处理 XmlDictionaryWriter
后,它正在写入的 MemoryStream
被关闭。
我的代码:
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(outputStream))
{
DataContractSerializer serializer = new DataContractSerializer(iObject.GetType());
serializer.WriteObject(writer, iObject);
writer.Flush();
}
//outputStream is closed now.
XmlWriterSettings says that the CloseOutput 属性 的文档默认为 false。
我无法将 XmlDictionaryWriter.CreateBinaryWriter
的重载与 ownsStream
参数一起使用,因为它在 PCL 中不可用。
如何让 XmlDictionaryWriter
在处理 XmlDictionaryWriter
后让输出 Stream
继续存在?
我认为唯一的选择是自己实施 XmlDictionaryWriter
class。
这可能看起来很难,但您所要做的就是创建 2 个新的 classes:
PersistentXmlDictionaryWriter
与 this one but instead of deriving from XmlWriter
would derive from your new class PersisternXmlWriter
that would be exaxtly like this one 完全相同,但您需要更改 Dispose 方法以不关闭流。
应该可以了。
鉴于超载XmlDictionaryWriter.CreateBinaryWriter(outputStream, null, null, false)
is unavailable on your version of the PCL, you have a couple of options similar to those described in Is there any way to close a StreamWriter without closing its BaseStream?:
您可以冲洗但不能处理 XmlDictionaryWriter
并将其留给 GC 进行清理。与 Dispose()
方法不同,终结方法 不会 处理底层流。
为您的流创建一个包装器 Stream
,该包装器在释放时不会释放底层流。例如。 Jon Skeet 在他的 MiscUtil 中有 NonClosingStreamWrapper
。
我的上下文是 .NET PCL Profile111。
我尝试使用 DataContractSerializer
和 XmlDictionaryWriter
进行二进制 xml 序列化。我遇到的问题是,在我处理 XmlDictionaryWriter
后,它正在写入的 MemoryStream
被关闭。
我的代码:
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(outputStream))
{
DataContractSerializer serializer = new DataContractSerializer(iObject.GetType());
serializer.WriteObject(writer, iObject);
writer.Flush();
}
//outputStream is closed now.
XmlWriterSettings says that the CloseOutput 属性 的文档默认为 false。
我无法将 XmlDictionaryWriter.CreateBinaryWriter
的重载与 ownsStream
参数一起使用,因为它在 PCL 中不可用。
如何让 XmlDictionaryWriter
在处理 XmlDictionaryWriter
后让输出 Stream
继续存在?
我认为唯一的选择是自己实施 XmlDictionaryWriter
class。
这可能看起来很难,但您所要做的就是创建 2 个新的 classes:
PersistentXmlDictionaryWriter
与 this one but instead of deriving from XmlWriter
would derive from your new class PersisternXmlWriter
that would be exaxtly like this one 完全相同,但您需要更改 Dispose 方法以不关闭流。
应该可以了。
鉴于超载XmlDictionaryWriter.CreateBinaryWriter(outputStream, null, null, false)
is unavailable on your version of the PCL, you have a couple of options similar to those described in Is there any way to close a StreamWriter without closing its BaseStream?:
您可以冲洗但不能处理
XmlDictionaryWriter
并将其留给 GC 进行清理。与Dispose()
方法不同,终结方法 不会 处理底层流。为您的流创建一个包装器
Stream
,该包装器在释放时不会释放底层流。例如。 Jon Skeet 在他的 MiscUtil 中有NonClosingStreamWrapper
。