1

I am using XmlReader.ReadInnerXml() to load part of an XML file and save it as an XmlDocument. I ran into OutOfMemoryException when the innerXml part was over 2 GB (an estimate). What is the best way to handle this error? Is there a better way to create a large xml from XmlReader? Can I save the content without loading into memory?

using (XmlReader xmlRdr = XmlReader.Create(file))
{
  xmlRdr.MoveToContent();
  while (xmlRdr.Read())
  {
    //when read to XmlNodeType.Element and xmlRdr.Name meets certain criteria
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.PreserveWhitespace = true;
    try
    {
       xmlDoc.LoadXml(xmlRdr.ReadInnerXml());
       //get a few data from within the innerXml and eventually use XmlWritter to save the file                                    
    }
    catch(Exception e)
    {
       string content = $"{e.GetType()} {e.Message} {NewLine} {objId}";
       //send content to log file and email
    }
  }
}
  • Instead of loading that huge file into memory, why not use a stream and read it from there ? – Software Dev Aug 25 '20 at 04:42
  • I need to get some data out of that innerXml, and eventually save that innerXml part as an xml file. Then, advance the read to the next node for the next innerXml. – Sunny Roland Aug 25 '20 at 14:05
  • Well, it doesn't change anything. You can read the file from a stream, make changes, then write it to a file. [Here's an example](https://stackoverflow.com/questions/17864254/modify-filestream) – Software Dev Aug 25 '20 at 14:18
  • You can use `XmlReader.ReadSubtree()` to read a scoped portion of an XML file directly, without needing to call `ReadInnerXml()` to convert the entire portion to a string. It corresponds to the XML returned by `ReadOuterXml()` so you will need to consume the outer element with `ReadStartElement()` before passing it to `xmlDoc.Load(subRdr)`. See https://dotnetfiddle.net/Z7S1pT and [ReadOuterXml is throwing OutOfMemoryException reading part of large (1 GB) XML file](https://stackoverflow.com/q/46603999/3744182) and [XmlReader read continually](https://stackoverflow.com/a/31062998/3744182). – dbc Nov 29 '20 at 17:25
  • That should save a lot of memory, and may answer your question. But if not, please take a look at [Automating replacing tables from external files](https://stackoverflow.com/q/28891440/3744182) and [Combining the XmlReader and XmlWriter classes for simple streaming transformations](http://blogs.msdn.com/b/mfussell/archive/2005/02/12/371546.aspx), and update your question with a [mcve]. – dbc Nov 29 '20 at 17:30

1 Answers1

0

As said in one of the comments maybe try using StreamReader and StreamWriter

This tutorial might help