Problem with stream in Aspose.Words

I’m having trouble opening a document with a stream using Aspose.Words…
I’m creating a document and saving its contents to a column in the database. I would then like to retreive the DB column and use its content to open the document at a later date…
I’m successfully using the following code to save the document to the DB::

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Test");
MemoryStream ms = new MemoryStream();
doc.Save(ms, SaveFormat.FormatDocument);
string dbColumn= System.Text.Encoding.ASCII.GetString(ms.ToArray());
I'm using the following code to open the document from a stream using the column from the database:
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(dbColumn);
MemoryStream ms2 = new MemoryStream(buffer);
Document doc2 = new Document(ms2, string.Empty, LoadFormat.FormatDocument, string.Empty);

Unfortunately, the call to Document() results in the following error: “This is not a structured storage file.”
Any idea why this is happening? Any help would be greatly appreciated…

It seems that you need to move the stream pointer to the beginning of the stream befor loading the document from it:

MemoryStream ms2 = new MemoryStream(buffer);
ms2.Seek(0, SeekOrigin.Begin);
Document doc2 = new Document(ms2);

Adding the call:
ms2.Seek(0, SeekOrigin.Begin)
prior to opening the document results in the same error message. In my previous testing, I had made this call but removed it since the value of m2.Position was already 0.
If I attempt to call Document doc2 = new Document(ms2) as you suggested, I get the following error message: “Unknown file format.”
I’m beginning to wonder if the following code for creating the memory stream from a string is at fault:

byte[] buffer = System.Text.Encoding.ASCII.GetBytes(rtf);
MemoryStream ms2 = new MemoryStream(buffer);

If I open the document with the original stream, as such:

Document doc2 = new Document(ms, string.Empty, LoadFormat.FormatDocument, string.Empty);

the call succeeds.
This is very strange…

ASCII encoding won’t work in this case because it truncates the most significant bit of the byte. Of all well-known encodings only Unicode encoding will work here but it will effectively double the size of the stored string. The other option is to convert memory stream to/from string using the following procedures:

// convert memory stream to string
StringBuilder sb = new StringBuilder((int)ms.Length);
foreach (byte b in ms.ToArray())
{

    sb.Append((char)b);
}
string dbColumn = sb.ToString();
// convert string to memory stream 
byte[] buffer = new byte[dbColumn.Length];
int i = 0;
foreach (char ch in dbColumn)
{

    buffer[i++] = (byte)ch;
}
MemoryStream ms2 = new MemoryStream(buffer);

Your procedures to convert memory stream to/from string proved useful.
My problem was compounded by the fact that the truncation of the most significant bits also happened when saving to a database column defined as a string/text type. After changing my DB column to an image/byte[] type, everything worked like a charm…
Thank you very much for your help…