开发者

FileInfo, StreamWriter, and StringBuilder. What encoding does this combination produce?

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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜