I have ASP.NET MVC application where I am selecting CSV File and parsing that file.
While Parsing, if a particular Cell is having Line Breaks, then it is treating as next record from the Line Break. As is this Attached image , in the first row, for StreetName column, it is treating as one Row upo Terminal, And "Chalwell" as another Row. How to handle LineBreaks in C# here?
My code is like this: Here ReadLine() method is used. Can we use any other method to consider whole cell as value and ignore LineBreaks?
public static List<T> ConvertCsvToDataTable<T>(StreamReader csvReader, ref Dictionary<int, List<string>> failures) where T : new()
{
var objects = new List<T>();
var columnIndex = new Dictionary<string, int>();
var headerLine = csvReader.ReadLine();
var headers = headerLine.Split(Convert.ToChar(","));
for (var i = 0; i < headers.Length; i++)
{
columnIndex.Add(headers[i], i);
}
var lineNumber = 1;
while (!csvReader.EndOfStream)
{
var line = csvReader.ReadLine();
if (line != null)
{
var obj = new T();
var csvRow = Regex.Split(line, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
var lineFailures = AssignValuesFromCsv(obj, csvRow, columnIndex);
if (lineFailures.Count > 0) failures.Add(lineNumber, lineFailures);
objects.Add(obj);
}
++lineNumber;
}
return objects;
}
Thanks Sai