1

I use a OleDb data reader to read a number of records, and then write them to a CSV. I then read from this CSV using File.ReadAllLines, then split on commas to get my data. The problem is some parts of the CSV include a character I can't display (shows up as a square), which appears to act as a line break - this line break corrupts the CSV, so I need to get rid of it.

I've tried replacing Environment.NewLine with something else (a blank space) when writing the CSV, and ditto with /r and /n but to no avail - the character isn't replaced. What other ways are there to remove these?

Dave M
  • 1,282
  • 1
  • 16
  • 27
Chris
  • 7,165
  • 21
  • 93
  • 185
  • Instead of parsing the CSV in your code, it really is best to leverage a parser library that has already been developed to handle discrepancies in encodings and edge cases with separators. You'll find this question useful: [Are there any CSV readers/writer libraries in C#?](http://stackoverflow.com/questions/1941392/are-there-any-csv-readers-writer-libraries-in-c) – Roy Goode Feb 11 '12 at 14:10
  • Post the details of that char, and the relevant code for input/output. – Henk Holterman Feb 11 '12 at 15:27

2 Answers2

5

I then read from this CSV using File.ReadAllLines, then split on commas to get my data.

Stop rolling your own CSV parser.

Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
1

Dont write CSV file's this way... as it won't work in every scenario.

Use OLD DB to do it for you.

http://devlicio.us/blogs/sergio_pereira/archive/2008/09/17/tip-export-to-csv-using-ado-net.aspx

Hope that helps.

Sebastian Siek
  • 2,035
  • 17
  • 16