读取从zip下载的大型Xml文件并加载到MemoryStream中

时间:2014-06-26 15:15:32

标签: c# xml xmlreader memorystream xelement

我正在阅读非常大的Xml文件(400多MB),15 MB压缩文件已下载并解压缩到MemoryStream。我每次都遇到System.OutOfMemoryException。我尝试使用StreamReader.ReadToEnd()并将其读取为字符串,但不起作用。

我用谷歌搜索并使用XmlReader并按照这里的帖子建议将其加载到XElement。但是,我仍然遇到OutOfMemoryException错误。

string downloadUrl = requestStatus.ReportDownloadUrl;

//create a network stream to the report Url
using (Stream reportZipStream = new WebClient().OpenRead(downloadUrl)) //download the file
using (Stream reportZipMemoryStream = new MemoryStream()) //initilize zip memorystream
using (Stream reportXmlStream = new MemoryStream()) //load xml file to memorystream for manipulation
{
    //copy zip file to memorystream
    reportZipStream.CopyTo(reportZipMemoryStream);
    reportZipMemoryStream.Seek(0, SeekOrigin.Begin);

    //unzip to Xml memory stream
    using (ZipFile reportZip = ZipFile.Read(reportZipMemoryStream))
    {
        reportZip[0].Extract(reportXmlStream);
    }

    reportXmlStream.Seek(0, SeekOrigin.Begin);

    Dictionary<string, object> parsedXml = default(Dictionary<string, object>);

    //read and parse
    if (reportXmlStream.CanRead && reportXmlStream.Length > 0)
    {
        XmlDataParser parser = new XmlDataParser();
        using (XmlReader reader = XmlReader.Create(reportXmlStream))
        {
            XElement elem = XElement.Load(reader); //out of memory error here
            parsedXml = parser.doParse(elem);
        }
    }

1 个答案:

答案 0 :(得分:1)

您不应该将DOM Parser(例如XElement)用于400 Mo XML文件。您应该使用SAX解析器。