Search code examples
c#csvparsing.net-corecsvhelper

Is it possible to overwrite default mapping of CsvHelper for a specific column, if a field under it is an empty string - to write something else?


If we have a csv file with lots of columns, one of which is HelloWorld, is there a way to make it so that when CsvHelper parses the file, if the field under the column HelloWorld is empty, instead of returning/writing an empty string, to write something else (for example null, or hello!).

Thank you!


Solution

  • Yes it is possible. You need to use the ClassMap or CsvClassMap it depends on which version are you using. ClassMap is the latest implementation. Here is an example for the HelloWorld column:

    sealed class HelloWorldDefinitionMap : ClassMap<HelloWorldRow>
    {
        public HelloWorldDefinitionMap()
        {
            Map(m => m.HelloWorld).Default("empty");
        }
    }