Having an problem where streamwriter is producing erroneous characters in a csv that I’m producing. The characters,  , only appear at the start of the file:
5,"GEN",555555555,,"Evan","Smith",,,,,,,,,,,,,,,,,,,,,,,,,"email@someplace.com"
5,"GEN",555555555,,"Dorathy","Smith",,,,,,,,,,,,,,,,,,,,,,,,,"email@someplace.com"
5,"GEN",555555555,,"Marvin","Smith",,,,,,,,,,,,,,,,,,,,,,,,,"email@someplace.com"
....
Looks to me like an encoding problem. I’ve tried different encoding and also a regex to clean the string with little luck. Here’s the code for preview (although it’s nothing complex). Also it’s running as a web process.
string fileName = includeEmail == false ? "attachment; filename=stuacct_batch.txt" : "attachment; filename=rentals.csv";
Context.Response.Clear();
Context.Response.AddHeader("content-disposition", fileName);
Context.Response.ContentType = includeEmail == false ? "text/plain" : "text/csv";
using (StreamWriter sw = new StreamWriter(Response.OutputStream, Encoding.UTF8))
{
string line = "";
foreach (Student s in students)
{
// ... some logic above that that's misc. to this problem
line = "5,\"GEN\"," + s.StudentNumber + ",,\"" + s.FirstName + "\",\"" + s.LastName + "\",,,,,,,,,,,,,,,,,,,,,,,,,\"" + s.Email + "\"";
sw.WriteLine(line);
}
}
Context.Response.End();
Context.Response.Flush();
Context.ApplicationInstance.CompleteRequest();
You have specified that you want to use UTF-8 encoding for the stream and those initial bytes is a valid UTF-8 Byte Order Mark (BOM). The problem is apparently that your viewer/editor doesn’t decode the UTF-8 stream correctly. If it only is the BOM that is the problem and you want to create a stream without a BOM you can create your own instance of the
UTF8Encodingclass:In case you really want to work with ASCII data you should use that encoding instead: