How to overcome OutOfMemoryException pulling large xml documents from an API?

前端 未结 3 1635
终归单人心
终归单人心 2020-12-03 16:08

I am pulling 1M+ records from an API. The pull works ok, but I\'m getting an out of memory exception when attempting to ReadToEnd into a string variable.

<
3条回答
  •  佛祖请我去吃肉
    2020-12-03 16:49

    It sounds like your file is too big for your environment. Loading the DOM for a large file can be problematic, especially when using the win32 platform (you haven't indicated whether this is the case).

    You can combine the speed and memory efficiency of XmlReader with the convenience of XElement/Xnode, etc and use an XStreamingElement to save the transformed content after processing. This is much more memory-efficient for large files

    Here's an example in pseudo-code:

        // use a XStreamingElement for writing
        var st = new XStreamingElement("root"); 
        using(var xr = new XmlTextReader(stream))
        {
            while (xr.Read())
            {
                // whatever you're interested in
                if (xr.NodeType == XmlNodeType.Element) 
                {
                    var node = XNode.ReadFrom(xr) as XElement;
                    if (node != null)
                    {
                        ProcessNode(node);
                        st.Add(node);
                    }
                }
    
            }
        }
        st.Save(outstream); // or st.WriteTo(xmlwriter);
    

提交回复
热议问题