tags:

views:

76

answers:

2

I have a list of string that I am writing out to a CSV file. I write this file like so:

FileInfo outputFile = new FileInfo("c:\output.csv");
StreamWriter writer = outputFile.CreateText();
StringBuilder line = new StringBuilder();

// List<String> listOfItems = ...
for (int i = 0, j = listOfItems.Count; i < j; i++) {
  line.AppendFormat(" {0}", listOfItems[i]);
}

writer.WriteLine("col1,col2,{0}", line.ToString().Trim(' '));
writer.Close();

When I examine c:\output.csv with tools like Notepad++ or enca the file looks to be plain text with a us-ascii encoding. But when my client uploads the CSV to their web store the column built with StringBuilder is in the following format:

:VAL1 :VAL2 :VAL3 ... :VALN

That column should actually be:

val1 val2 val3 ... valN

Notice that the incorrect line has colons prefixed to each value, and the values have been uppercased.

The only thing I can figure is an encoding problem. So, what encoding would the above code generate?

Update (1 September 2010): Turned out to be a display bug in the remote web store. This code outputs plain text as it should.

+2  A: 

I don't think this is an encoding problem. I would use some kind of network monitor (like NetMon or Fiddler) to verify the data that is being transfered is what you expect. My guess is that the web store is manipulating the data after you upload it.

David
A: 

I've run the code you've presented and reviewed the file produced using a Hex Editor, it's been encoded in UTF8/ASCII as the only content I can see in the file is the text output (exactly as written) plus a CR+LF combination caused by the fact that you're using WriteLine. Whatever is causing the file to transform into :VAL1 :VAL2 :VAL3 is nothing to do with your code.

That said, the line for (int i = 0; j = listOfItems.Count; i < j; i++) didn't compile for me, I had to tweak it to for (int i = 0; i < listOfItems.Count; i++) - are you sure that's what's in your production code?

Rob
There was a typo in the for declaration. I fixed it.I just opened my output file with a hex editor as well and I agree. It looks like plain ASCII text like I intended.
jsumners
@jsumners, I think we can be quite confident that your clients web store is doing something, and I quite, "bat s*it mental" with the files that are uploaded to it =) There's no way anything encoding related could be doing what you've described in your question! =) Something tells me it's a process that occurs once the file has been deposited on their "web store", rather than "during transfer", so I don't know how much use, if any, something like Fiddler or Wireshark would be...
Rob
I completely agree. But I don't work in .Net very often so I just wanted to make sure my assumption was correct.
jsumners