1

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

Rita
  • 43
  • 4
  • 2
    As you have discovered, there is more to CSV files than just separating values with commas. The file needs to be properly output so that the line breaks you speak of are contained within delimiters and a proper parsing routine is needed to extract the information. You might be better off finding a component out there that you can reference and use. Also, make sure to check out the `TextFieldParser` class: https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx. – JuanR May 14 '18 at 20:48
  • It is probably easier to use Oledb to read the excel worksheet directly then to go to an intermediate csv file. Oledb will not give the extra rows. – jdweng May 14 '18 at 20:51
  • 1
    [Stop writing your own CSV parser](http://www.secretgeek.net/csv_trouble); the format is more complicated than you realize. There are plenty of working [CSV parsers](https://stackoverflow.com/questions/2081418/) ready for you to drop in to your application. – Dour High Arch May 14 '18 at 21:12

0 Answers0