XmlReader - 如何在没有 System.OutOfMemoryException 的情况下读取元素中的超长字符串

XmlReader - How to read very long string in element without System.OutOfMemoryException

我必须读取从 API.

返回的 XML 元素中的文件内容 Base64 字符串

我的问题是这个字符串可能很长,具体取决于文件大小。

起初,我用XmlDocument来阅读XML。现在我使用 XmlReader 来避免 System.OutOfMemoryException 当 XML 太大时。

但是我在读取字符串时也收到了 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) 来达到这个目的。此方法允许以块的形式读取和解码 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);

演示 fiddle here.