I have a row of data in a csv where some of the cells may contain line breaks
I'm uploading this file using an Asp:FileUpload and trying to read through each line with a StreamReader:
var file = btnFileUpload.PostedFile;
using (StreamReader sr = new StreamReader(file.InputStream))
{
string currentLine;
var line = 1;
// currentLine will be null when the StreamReader reaches the end of file
while ((currentLine = sr.ReadLine()) != null)
{
....do stuff...
}
}
However, in debugging I found that sr.ReadLine()
is breaking the lines at the line breaks within the cells, such as in the Category cell. For example, when I read line 2 (the first line of data after the header), the value is:
"/Home/Blog/2018/november/power,English : English,Erica Stockwell-Alpert,/Home/Blog/Categories/Accounts Payable Automation;"
and then the next sr.ReadLine():
"/Home/Blog/Categories/Financial Services;"
and then
"/Home/Blog/Categories/Robotic Process Automoation,<p>[the rest of the line]"
How can I prevent sr.ReadLine() from breaking on the new line characters within cells? Or if I can't, how else can I read the file line by line?
Note: I cannot use a csv reader ClassMap anad csvReader.GetRecords because the tool I am working on needs to be able to handle any different fields in the header, it is not associated with one specific class. So I need to read through the file line by line.