I am trying to read a csv file using the following code. Its throwing an exception if there is double quotes (") in the data. I want to handle all the special chars while reading.
using (TextReader reader = new StreamReader("C:\\test\test.csv"))
{
using (CsvReader csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Read();
csv.ReadHeader();
while (csv.Read())
{
var result= csv[0].Split(';');
}
}
}
My CSV file: The csv.Read is failing line at line# 3
Name;Location;State;Country
Keka1;Lansing; MI; USA
Keka2;"Ohio"; OH; USA // Error at this line
Keka3;Lansing; MI; USA
I do not want to escape quotes, but want to include it. Please advice.
Which CsvReader
class are you using? (I authored a class like this called SoftCircuits.CsvParser.)
Either way, you aren't using it correctly. The code you have could just use a regular input stream because you seem to be reading a line at a time and then parsing it. CSV readers will parse it for you and they will handle fields with double quotes.