In CsvHelper how to catch a conversion error and know what field and what row it happened in?

后端 未结 3 718
不思量自难忘°
不思量自难忘° 2021-01-01 10:02

I am using the class CsvReader successfully and am happy with it, however, the file that I consume is being produced by a group which changes column formats wit

3条回答
  •  猫巷女王i
    2021-01-01 10:31

    Currently, there is no way to ignore errors at the field/property level. Your current options are these:

    Look at the exception data.

    catch( Exception ex )
    {
        // This contains useful information about the error.
        ex.Data["CsvHelper"];
    }
    

    Ignore reading exceptions. This is on a row level, though, not field. It will allow the whole file to still be read, and just ignore the rows that don't work. You can get a callback when an exception occurs.

    csv.Configuration.IgnoreReadingExceptions = true;
    csv.Configuration.ReadingExceptionCallback = ( ex, row ) =>
    {
        // Do something with the exception and row data.
        // You can look at the exception data here too.
    };
    

提交回复
热议问题