XmlMtomReader读取策略
考虑以下代码:
Stream stream = GetStreamFromSomewhere();
XmlDictionaryReader mtomReader =XmlDictionaryReader.CreateMtomReader
(
stream,
Encoding.UTF8,
XmlDictionaryReaderQuoatas.Max
);
/// ...
/// is there best way to read binary data from mtomReader's element??
string elementString = mtomReader.XmlReader.ReadElementString();
byte[] elementBytes = Covert.FromBase64String(elementString);
Stream elementFileStream = new FileStream(tempFileLocation);
elementFileStream.Write(elementBytes,0,elementBytes.Length);
elementFileStream.Close();
/// ...
mtomReader.Close();
问题是二进制附件的大小有时会超过 100Mb。有没有办法逐块读取元素的二进制附件,然后将其写入临时文件流,这样我就可以避免为空洞的东西分配内存?
第二个 - 更具体的问题 - 在我读取元素的内容之前,mtomReader 是否会创建 mime 二进制附件的任何内部缓存,即为二进制数据分配内存?还是直接从输入流读取字节?
Consider the following code:
Stream stream = GetStreamFromSomewhere();
XmlDictionaryReader mtomReader =XmlDictionaryReader.CreateMtomReader
(
stream,
Encoding.UTF8,
XmlDictionaryReaderQuoatas.Max
);
/// ...
/// is there best way to read binary data from mtomReader's element??
string elementString = mtomReader.XmlReader.ReadElementString();
byte[] elementBytes = Covert.FromBase64String(elementString);
Stream elementFileStream = new FileStream(tempFileLocation);
elementFileStream.Write(elementBytes,0,elementBytes.Length);
elementFileStream.Close();
/// ...
mtomReader.Close();
The problem is that the size of the binary attachment supposed to be over 100Mb sometimes. Is there a way to read element's binary attachment block by block and then write it to the temporary file stream so i can escape from allocating memory for the hole stuff?
The second - even more specific issue - does mtomReader create any internal cache of the mime binary attachment before i read element's content, i.e. allocate memory for binary data? Or does it read bytes from the input stream directly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于那些可能对解决方案感兴趣的人:
ReadElementContentAsBase64(...) 有助于逐块读取二进制部分。我的帖子的第二个问题在这里完美地涵盖了: XmlMtomReader 在内部缓存来自输入流的二进制数据?
For those who may be interested in the solution:
ReadElementContentAsBase64(...) helps read binary parts block by block. The second issue of my post was covered perfectly here: Does XmlMtomReader cache binary data from the input stream internally?
对于这种大小的附件,最好使用流式传输。
请参阅:http://msdn.microsoft.com/en-us/library/ ms731913.aspx
For an attachment of that size it would be better to use streaming.
See: http://msdn.microsoft.com/en-us/library/ms731913.aspx
首先,您的代码应该更像这样:
如果没有
using
块,您将面临资源泄漏的风险。To begin with, your code should be more like this:
Without the
using
blocks, you're at risk of resource leaks.