[英]XmlReader - How to read very long string in element without System.OutOfMemoryException
我必须读取从API返回的XML元素中的文件内容Base64字符串。
我的问题是此字符串可能很长,具体取决于文件大小。
最初,我使用XmlDocument
读取XML。 现在,当XML太大时,我使用XmlReader
来避免System.OutOfMemoryException
。
但是我在读取字符串时也收到了System.OutOfMemoryException
。 我猜这串太长了。
using (XmlReader reader = Response.ResponseXmlXmlReader)
{
bool found = false;
//Read result
while (reader.Read() && !found)
{
if(reader.NodeType == XmlNodeType.Element && reader.Name == "content")
{
//Read file content
string file_content = reader.ReadElementContentAsString();
//Write file
File.WriteAllBytes(savepath + file.name, Convert.FromBase64String(file_content));
//Set Found!
found = true;
}
}
}
如何在没有System.OutOfMemoryException
情况下使用XmlReader
读取文件内容字符串?
为此XmlReader.ReadElementContentAsBase64(Byte[] buffer, Int32 index, Int32 count)
可以使用XmlReader.ReadElementContentAsBase64(Byte[] buffer, Int32 index, Int32 count)
。 此方法允许以块的形式读取和解码XML元素的Base64元素内容,从而避免大型元素出现OutOfMemoryException
。
例如,您可以引入以下扩展方法:
public static class XmlReaderExtensions
{
public static bool ReadToAndCopyBase64ElementContentsToFile(this XmlReader reader, string localName, string namespaceURI, string path)
{
if (!reader.ReadToFollowing(localName, namespaceURI))
return false;
return reader.CopyBase64ElementContentsToFile(path);
}
public static bool CopyBase64ElementContentsToFile(this XmlReader reader, string path)
{
using (var stream = File.Create(path))
{
byte[] buffer = new byte[8192];
int readBytes = 0;
while ((readBytes = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, readBytes);
}
}
return true;
}
}
然后执行:
var path = Path.Combine(savepath, file.name);
var found = reader.ReadToAndCopyBase64ElementContentsToFile("content", "", path);
演示在这里摆弄。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.