CsvHelper parsing fails for tab delimitted record

Ok, the problem is that one of your fields contains a ", which is a quotation character. You either have to escape it, or ignore quotes in your file. In the code bellow I’m ignoring. Also, if you’ll be using Parser.Read, you shouldn’t use csv.Read since it already advances to the next record before reading. So, your code becomes:

private void TestCSvParser(string fullFilePath)
{    
    try
    {
        string ext = Path.GetExtension(fullFilePath);
        using var reader = new StreamReader(fullFilePath);
        using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
        csv.Configuration.BadDataFound = BadDataFound;
        csv.Configuration.DetectColumnCountChanges = true;
        if (ext.Contains(".txt"))
        {
            csv.Configuration.Delimiter = "\t";                    
            csv.Configuration.IgnoreQuotes = true;
        }
        string[] record;
        while((record = csv.Parser.Read()) != null)
        {
            var fullrow = csv.Context.RawRecord;
        }                
    }
    catch (Exception ex)
    {

    }
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top