1

I have a random exception when trying to retrieve and load an XML file through SOAP protocol like this

XmlDocument xml = new XmlDocument();
using (Stream stream = webRequest.GetResponse().GetResponseStream())
{
   xml.Load(stream); //<-- Exception here
}

I did test of files with a size of 267MB. It is a system integration where a system generate a XML with all the employees' data including picture of their profiles. Everything worked well. But recently I am experiencing some exceptions with this detail:

at System.Text.StringBuilder.ToString()
at System.Xml.XmlTextReaderImpl.ParseText()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.Load(Stream inStream)

I wonder if there is a size limit on each node. The file only contained data with a size of 15MB, but once the file included in a node a picture in base64 the size increased to 260M, but still worked well on tests and production.

It is lately that I have this problem.

Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95
  • take a look here - https://stackoverflow.com/questions/137285/what-is-the-best-way-to-read-getresponsestream – MethodMan Dec 01 '17 at 16:07
  • 1
    Use `XmlReader` instead of `XmlDocument`. It's a forward-only parser which does not require all of the data to be in memory at one time. See [Deciding on when to use XmlDocument vs XmlReader](https://stackoverflow.com/questions/1505075/deciding-on-when-to-use-xmldocument-vs-xmlreader) – Roman Marusyk Dec 01 '17 at 16:10
  • How long does it take to get results? I would monitor the Task Manager while running to see if there is a memory issue. Is it possible that you are running application more than once? Are other users connecting to server at same time? – jdweng Dec 01 '17 at 20:34
  • I made a debug and I discovered that it is not there the exception. After loading, I perform a loop in read the nodes and I get the exception during the loop randomly. – Maximus Decimus Dec 01 '17 at 22:20
  • Rather than using `XmlDocument`, deserialized to a fixed c# data model using `XmlSerializer` -- and simply omit a property corresponding to the image. This will cause the image elements to get skipped during deserialization. Deserializing to fixed types will also save some memory taken by DOM overhead. – dbc Dec 01 '17 at 22:36

0 Answers0