views:

1204

answers:

4

code snippet:

//byte[] myByteArray = byte array from database (database BLOB) 
myByteArray = (byte[]) ((dbCommand.Parameters["parameter"].Value));

string myString =System.Text.Encoding.UTF8.GetString(myByteArray);

Xmldocument doc = new Xmldocument();
doc.Load(myString);

============

I am getting System.OutOfMemoryException sometimes.

string myString = System.Text.Encoding.UTF8.GetString(myByteArray);

when converting bytearray to string i am getting this error.

is there a way i can make this code robust.

All i am trying to do is loading the BLOB in byte array and then converting them to string and loading them in xmldocument to work with.

+1  A: 

XmlDocument.Load(String) tries to load the XML document from the URL given as parameter, i.e. it tries to interpret your possibly HUGE string as an URL. No wonder that something goes wrong.

Use LoadXml() instead.

Michael Borgwardt
+6  A: 

If you've got a string containing XML text, you actually want XmlDocument.LoadXML. XmlDocument.Load treats the string as a URL.

That said, XmlDocument.Load has overloads taking an XmlReader, a TextReader or a Stream. You can create a MemoryStream on the underlying byte array, and pass that; this avoids the string conversion.

Roger Lipscombe
Didn't know about the MemoryStream class. Just what I needed! Thanks.
bounav
A: 

It sounds like your blob is just too big to fit in memory as a string. Don't forget that ASCII values will be twice the size as a string that they are in binary form. How big are these values, anyway?

What do you need to do with the string afterwards? You show loading it as XML (although as Roger said, you really want LoadXml) but not what you were planning on doing after that. Could you do something which streams the data instead? If so, wrap the byte array in a MemoryStream and then use a StreamReader on top of that.

Jon Skeet
A: 

Strings/StringBuilders require contiguous memory block, so the OutOfMemoryException is closely related to the fragmentation of the heap.

That being said, you'll need to chunk the input or as already suggested use streams.

arul