Adrichem.Serialization.CsvSerializer 3.0.0

Serialize and deserialize .csv files.

Install-Package Adrichem.Serialization.CsvSerializer -Version 3.0.0
dotnet add package Adrichem.Serialization.CsvSerializer --version 3.0.0
<PackageReference Include="Adrichem.Serialization.CsvSerializer" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Adrichem.Serialization.CsvSerializer --version 3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Serialize and deserialize .csv files

Inspired by https://gist.github.com/caschw/ddac05f58f1f081bd9da

  1. Can serialize anonymous classes in addition to regular classes.
  2. Supports fields and properties.
  3. Supports culture specific formatting of values.
  4. Configurable separator, rownumbers
  5. Separate NuGet adds extension methods for usage in LINQ pipelines

Serialization example

var Data = ...Some IEnumerable...
var SerializationOptions = new CsvSerializationOptions
{
    Separator = ';',
    Culture = CultureInfo.GetCultureInfo("nl-nl")
};

var Output = new MemoryStream();
using (var Writer = new StreamWriter(Output, new System.Text.UTF8Encoding(true), 1024, true))
{
    CsvSerializer.Serialize(Writer, Data, SerializationOptions);
}

Deserialization example

string ENUSTest = "Double;Float;Date\n1.1;2.2;12/23/2018 12:00:00 AM";

var DeserializationOptions = new CsvDeserializationOptions
{
    Culture = CultureInfo.GetCultureInfo("en-us"),
    Separator = ';'
};
using( var Reader = new StreamReader(getInputStream(ENUSTest), Encoding.UTF8); )
{
    var Data = CsvSerializer.Deserialize<HasLocalizable>(Reader, DeserializationOptions);
}

Extension method example

//Dont forget to Install NuGet Package Adrichem.Serialization.CsvSerializer.ExtensionMethods
using Adrichem.Serialization.CsvSerializer.ExtensionMethods;

var SerializationOptions = new CsvSerializationOptions
{
    Separator = ';',
    Culture = CultureInfo.GetCultureInfo("nl-nl"),
};

var Data = new List<MyClass> 
{ 
    new MyClass { Prop1 = "1" , Prop2 = "A"},  
    new MyClass { Prop1 = "2" , Prop2 = "B"}, 
    new MyClass { Prop1 = "3" , Prop2 = "C"}, 
};
Data
    .OrderBy( item => item.Prop1)
    .CsvSerialize<MyClass>("c:\\temp\\ascending.csv", new System.Text.UTF8Encoding(true))
    .OrderByDescending(item => item.Prop2)
    .CsvSerialize<MyClass>("c:\\temp\\descending.csv", new System.Text.UTF8Encoding(true), SerializationOptions)
;

Serialize and deserialize .csv files

Inspired by https://gist.github.com/caschw/ddac05f58f1f081bd9da

  1. Can serialize anonymous classes in addition to regular classes.
  2. Supports fields and properties.
  3. Supports culture specific formatting of values.
  4. Configurable separator, rownumbers
  5. Separate NuGet adds extension methods for usage in LINQ pipelines

Serialization example

var Data = ...Some IEnumerable...
var SerializationOptions = new CsvSerializationOptions
{
    Separator = ';',
    Culture = CultureInfo.GetCultureInfo("nl-nl")
};

var Output = new MemoryStream();
using (var Writer = new StreamWriter(Output, new System.Text.UTF8Encoding(true), 1024, true))
{
    CsvSerializer.Serialize(Writer, Data, SerializationOptions);
}

Deserialization example

string ENUSTest = "Double;Float;Date\n1.1;2.2;12/23/2018 12:00:00 AM";

var DeserializationOptions = new CsvDeserializationOptions
{
    Culture = CultureInfo.GetCultureInfo("en-us"),
    Separator = ';'
};
using( var Reader = new StreamReader(getInputStream(ENUSTest), Encoding.UTF8); )
{
    var Data = CsvSerializer.Deserialize<HasLocalizable>(Reader, DeserializationOptions);
}

Extension method example

//Dont forget to Install NuGet Package Adrichem.Serialization.CsvSerializer.ExtensionMethods
using Adrichem.Serialization.CsvSerializer.ExtensionMethods;

var SerializationOptions = new CsvSerializationOptions
{
    Separator = ';',
    Culture = CultureInfo.GetCultureInfo("nl-nl"),
};

var Data = new List<MyClass> 
{ 
    new MyClass { Prop1 = "1" , Prop2 = "A"},  
    new MyClass { Prop1 = "2" , Prop2 = "B"}, 
    new MyClass { Prop1 = "3" , Prop2 = "C"}, 
};
Data
    .OrderBy( item => item.Prop1)
    .CsvSerialize<MyClass>("c:\\temp\\ascending.csv", new System.Text.UTF8Encoding(true))
    .OrderByDescending(item => item.Prop2)
    .CsvSerialize<MyClass>("c:\\temp\\descending.csv", new System.Text.UTF8Encoding(true), SerializationOptions)
;

  • .NETStandard 2.0

    • No dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.0.0 137 3/12/2019
2.0.0 257 7/28/2018