Concepts

CsvMapping

The CsvMapping defines the mapping between the CSV column index and the properties of a .NET object. It is an abstract base class, that needs to be implemented and it exposes the MapProperty method to define the mapping.

You have seen an example for a CsvMapping in the Quickstart document.

private class CsvPersonMapping : CsvMapping<Person>
{
    public CsvPersonMapping()
        : base()
    {
        MapProperty(0, x => x.FirstName);
        MapProperty(1, x => x.LastName);
        MapProperty(2, x => x.BirthDate);
    }
}

CsvParserOptions

In order to parse a CSV file, you need to define the column delimiter to use and define to skip the header or not. These options are passed into a CsvParser by using the CsvParserOptions. The most basic constructor for a CsvParserOption is:

CsvParserOptions(bool skipHeader, char fieldsSeparator)

There are more advanced options, which can be passed with:

CsvParserOptions(bool skipHeader, string commentCharacter, ITokenizer tokenizer, int degreeOfParallelism, bool keepOrder)

The Parameters are:

  • skipHeader
    • Signals to skip the header row (true) or not (false).

  • tokenizer
    • The Tokenizer to use for splitting a line in the CSV data into the column values. The default is a StringSplitTokenizer (see User Guide on Tokenizer for additional information).

  • degreeOfParallelism
  • keepOrder
    • When the input is processed in parallel, the results can be unordered. The keepOrder flag signals wether to keep the original order (true) or return the unordered results (false). The default is true.

CsvReaderOptions

When reading CSV data from a string with CsvParser.ReadFromString, you have to define the NewLine character used for splitting the input into lines. This class is not neccessary when reading from a file with CsvParser.ReadFromFile.

The CsvReaderOptions constructor signature is:

public CsvReaderOptions(string[] newLine)

The parameter is:

  • newLine
    • Defines the character used for splitting the input data into lines.

CsvMappingResult

The CsvMappingResult is the result of the parsing. The class contains either the populated object or an error. Why doesn’t a CsvParser return just the mapped entities? Because the input data is processed in parallel and the CsvParser can’t stop parsing, because a single line has an error.

That’s why the parsed entity is wrapped in a CsvMappingResult, which holds either the entity or an error, that may have occured during parsing a CSV line.

You can check, if a CsvMappingResult is valid by checking the property CsvMappingResult<TEntity>.IsValid. If the CsvMappingResult is valid, then it contains the populated entity in the property. If the parsing was not possible due to an error, then the property CsvMappingResult<TEntity>.Error is filled.

You have seen this in the Quickstart example already.