2

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

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Didn't know about the MemoryStream class. Just what I needed! Thanks.
1

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.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.