Sheleski.DelimitedFile 0.1.0.6

dotnet add package Sheleski.DelimitedFile --version 0.1.0.6
NuGet\Install-Package Sheleski.DelimitedFile -Version 0.1.0.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sheleski.DelimitedFile" Version="0.1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sheleski.DelimitedFile --version 0.1.0.6
#r "nuget: Sheleski.DelimitedFile, 0.1.0.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Sheleski.DelimitedFile as a Cake Addin
#addin nuget:?package=Sheleski.DelimitedFile&version=0.1.0.6

// Install Sheleski.DelimitedFile as a Cake Tool
#tool nuget:?package=Sheleski.DelimitedFile&version=0.1.0.6

Sheleski.DelimitedFile

Library for parsing and saving CSV and Tab Delimited Files in .NET.

Usage

The CsvFile has a very simple API with Headers and Values properties. Use these properties to work with the data in the CSV file.

CsvFile csv = new CsvFile();

csv.Headers = new string[]{ "First Name", "Last Name" };
csv.Values = new string[][]
{
  new string[]{"Joe", "Blow"},
  new string[]{"Bonehead", "McGee"}
};

Save the CSV to file

csv.Save("c:\\temp\\names.csv");

Write the CSV to any arbitrary TextReader

using (var writer = new StringWriter())
{
    csv.Write(writer);

    string csvString = writer.ToString();
}

Load a CSV from a stream

WebRequest request = HttpWebRequest.Create("https://raw.githubusercontent.com/forxer/languages-list/master/src/Languages.csv");
using (WebResponse response = await request.GetResponseAsync())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
  var csvFile = CsvFile.Load(reader, CsvFileLoadOptions.WithHeaders);
}

Create a Delimited File from any IEnumerable easily.

You can easily generate a CSV file from a collection.

Start with a Person class.

class Person
{
    [Display(Order = 1, Name = "First Name")]
    public string FirstName { get; set; }

    [Display(Order = 2, Name = "Last Name")]
    public string LastName { get; set; }

    [Display(Order = 3, Name = "Birth Date")]
    public DateTime BirthDate { get; set; }

    [Display(Order = 4, Name = "Death Date")]
    public DateTime? DeathDate { get; set; }

    [Display(Order = -1)]
    public string InvisibleInfo { get; set; }

    [Display(Order = 4)]
    public List<Person> Children { get; } = new List<Person>();
}

Now lets create some people and make a CSV File from it.

First make sure to include the using directive.

using Sheleski.DelimitedFile;

Now we can make use of the .ToCsvFile() extension method.

Person[] people = new Person[]
{
    new Person{FirstName = "Elvis", LastName = "Presley", BirthDate = new DateTime(1935, 1, 8), DeathDate= null, InvisibleInfo = "Dead??"},
    new Person{FirstName = "Abraham", LastName = "Lincoln", BirthDate = new DateTime(1809, 2, 12), DeathDate = new DateTime(1865, 4, 15), InvisibleInfo = "Dead"},
};

CsvFile csvFile = people.ToCsvFile();

Resulting CSV string:

First Name,Last Name,Birth Date,Death Date,Children
Elvis,Presley,1/8/1935 12:00:00 AM,,System.Collections.Generic.List`1[Sheleski.DelimitedFile.Tests.CsvFile_EnumerableExtensions+Person]
Abraham,Lincoln,2/12/1809 12:00:00 AM,4/15/1865 12:00:00 AM,System.Collections.Generic.List`1[Sheleski.DelimitedFile.Tests.CsvFile_EnumerableExtensions+Person]

Automatic Column Mapping

If you do not pass a mapping to the .ToCSVFile() extension method, then it will generate an automatic mapping by looking at the DisplayAttributes assigned to the properties.

The Order property of the DisplayAttribute determines the order of the columns generated in the resulting Delimited File.

Note: If you do not want a column to be added to the resulting delimited file, the Order property can be set to a negative value.

The Name property of the DisplayAttribute determines the column header generated for the associated property.

The values used to populate the delimted file are generated by simply calling the ToString() method of each property.

Custom Column Mapping

DelimitedFileObjectMapping<Person> mapping = new DelimitedFileObjectMapping<Person>();
mapping.AddProperty(x => x.BirthDate).WithValue(d => d.ToShortDateString()).WithHeader("B-Day");
mapping.AddProperty(x => x.LastName);
mapping.AddProperty(x => x.FirstName).WithHeader("F Name");
mapping.AddProperty(x => x.DeathDate);

mapping.Add("Row Index", (x, i) => i.ToString());


CsvFile csvFile = people.ToCsvFile(mapping);

The example above shows how to add mappings to generate a CSV file by adding the Properties. You can also add computed values as well (See the Row Index example above).

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.1 is compatible.  netstandard1.2 is compatible.  netstandard1.3 is compatible.  netstandard1.4 is compatible.  netstandard1.5 is compatible.  netstandard1.6 is compatible.  netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net35 is compatible.  net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Sheleski.DelimitedFile:

Package Downloads
Sheleski.DelimitedFile.MvcCore

Helpers for Sheleski.DelimitedFile for ASP.NET MVCCore.

Sheleski.DelimitedFile.Mvc

Helpers for Sheleski.DelimitedFile for ASP.NET MVC.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.0.6 125 1/10/2024
0.1.0.5 609 12/7/2020
0.1.0.4 734 11/23/2020
0.1.0.2 405 11/23/2020
0.1.0.1 352 11/11/2020
0.1.0 430 11/10/2020