CsvConverterParseTResult(String, CsvMapping, FuncObject, TResult, Char, Boolean, CsvOpts) Method

Parses a CSV-String.

Definition

Namespace: FolkerKinzel.CsvTools.Mappings
Assembly: FolkerKinzel.CsvTools.Mappings (in FolkerKinzel.CsvTools.Mappings.dll) Version: 1.1.0+1263e8243dc2cd78095f678f813d7d9c52ea4315
C#
public static TResult[] Parse<TResult>(
	string csv,
	CsvMapping mapping,
	Func<Object, TResult> conversion,
	char delimiter = ',',
	bool isHeaderPresent = true,
	CsvOpts options = CsvOpts.Default
)

Parameters

csv  String
The CSV-String to parse.
mapping  CsvMapping
The CsvMapping used to convert the CSV data.
conversion  FuncObject, TResult

A function that converts the content of mapping to an instance of TResult.

The function is called for each row in csv and gets the CsvMapping as argument, filled with the current CsvRecord instance. The CsvMapping is passed to the function as dynamic argument: Inside the function the registered DynamicProperty instances can be used like regular .NET properties, but without IntelliSense ("late binding").

delimiter  Char  (Optional)
The field separator character used in csv.
isHeaderPresent  Boolean  (Optional)
true, to interpret the first line as a header, otherwise false.
options  CsvOpts  (Optional)
Parsing options.

Type Parameters

TResult
Generic type parameter that specifies the Type of the items in the array that the method returns.

Return Value

TResult
An array of TResult instances, initialized from the parsed csv.

Remarks

Example

  Note

In the following code examples - for easier readability - exception handling has been omitted.

Object serialization with CSV:

C#
using FolkerKinzel.CsvTools.Mappings;
using FolkerKinzel.CsvTools.Mappings.TypeConverters;

namespace Benchmarks;

internal static partial class CalculationReader
{
    internal static IList<Calculation> ReadDefault(string csv)
    {
        var doubleConverter = new DoubleConverter();

        CsvMapping mapping = CsvMappingBuilder
            .Create()
            .AddProperty("First", doubleConverter)
            .AddProperty("Operator", new CharConverter())
            .AddProperty("Second", doubleConverter)
            .AddProperty("Result", doubleConverter)
            .Build();

        return CsvConverter.Parse(
            csv,
            mapping,
            static mapping => new Calculation(mapping.First,
                                              mapping.Operator,
                                              mapping.Second,
                                              mapping.Result));
    }
}

Exceptions

ArgumentNullExceptioncsv, or mapping, or conversion is null.
CsvFormatExceptionInvalid CSV. The interpretation depends on options.
FormatException Parsing fails and Throwing is true.

See Also