Tuesday, May 01, 2007 1:06 AM
dbottjer
Web Services and UTF8Encoded ByteArrays
Our business partner began reporting all bullet points were being converted to question marks during the archiving of Word Docs. An early hunch that perhaps our FTP component was defaulting to ASCII transfer mode instead of Binary proved incorrect. With this option ruled out we knew our electronic document repository was properly receiving both the control file and the Word Doc. We began to suspect the web service exposed by our application which returns a document and metadata for archiving. We were using a byte array to store, in this case, the word document and write it to the http response stream. However, we were not specifying the encoding type which was our fatal mistake.
1: StringWriter sw = new StringWriter();
2: XmlTextWriter xw = new XmlTextWriter(sw);
3:
4: // Save Approval Document to Text Writter.
5: doc.WriteTo(xw);
6:
7: // Added this line to specify encoding and resolve bullet point issue.
8: UTF8Encoding encoding = new UTF8Encoding();
9:
10: // Convert Approval Document To Byte Array.
11: byte[] docAsBytes = encoding.GetBytes(sw.ToString());
12:
13: vvDoc.FileBinary = docAsBytes;
Filed under: ASP.NET, C#, System Integration, Debugging