This project has moved and is read-only. For the latest updates, please go here.

Encoding on the SaveAs method?

Mar 5, 2012 at 12:48 AM

Just wondering what encoding is used when saving to a memory stream?My scenario im trying to test is to save the excel file as an attachment (CRM note).When i say to disk, its fine, when i attach it the notes, its corrupted. Inside the file the default encoding states its UTF8, which i have set too but still getting issues. I will paste the first few lines to see how things differ already.

Full working excel spreadsheet saved to disk:

PK    ­d@VÆs  º    xl/workbook.xml ¢ (                      ì½`I–%&/mÊ{JõJ×àt¡€`$Ø

None working using memory stream etc:

PK\0\0\0\b\0��d@V�s\0\0�\0\0\0\0xl/workbook.xml �\0(�\0\0\0\0\0\0

Clearly its some of the characters it cant recognise and replaces them with null (\0 etc) i think?

Ive done this in two ways, both with the same issue:

            MemoryStream ms = new MemoryStream();         
            ms.Position = 0;

            var sr = new StreamReader(ms,Encoding.UTF8, true);
            var myStr = sr.ReadToEnd();

            byte[] contentBuffer = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(contentBuffer, 0, contentBuffer.Length);
            string myStr2 = Encoding.UTF8.GetString(contentBuffer);

Any help/suggestions appreciated!


Mar 5, 2012 at 4:52 PM

The xml files are UTF8 encoded but the file itself is a zip binary so you can't read it as if was text.