C# StreamReader.ReadLine() - Need to pick up line terminators

Posted by Tony Trozzo on Stack Overflow See other posts from Stack Overflow or by Tony Trozzo
Published on 2009-03-20T20:07:19Z Indexed on 2010/03/12 21:47 UTC
Read the original article Hit count: 940

Filed under:
|
|
|
|

I wrote a C# program to read an Excel .xls/.xlsx file and output to CSV and Unicode text. I wrote a separate program to remove blank records. This is accomplished by reading each line with StreamReader.ReadLine(), and then going character by character through the string and not writing the line to output if it contains all commas (for the CSV) or all tabs (for the Unicode text).

The problem occurs when the Excel file contains embedded newlines (\x0A) inside the cells. I changed my XLS to CSV converter to find these new lines (since it goes cell by cell) and write them as \x0A, and normal lines just use StreamWriter.WriteLine().

The problem occurs in the separate program to remove blank records. When I read in with StreamReader.ReadLine(), by definition it only returns the string with the line, not the terminator. Since the embedded newlines show up as two separate lines, I can't tell which is a full record and which is an embedded newline for when I write them to the final file.

I'm not even sure I can read in the \x0A because everything on the input registers as '\n'. I could go character by character, but this destroys my logic to remove blank lines.

Any ideas would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about c#

Related posts about streamreader