Sion.Useful.Files
1.1.0
See the version list below for details.
dotnet add package Sion.Useful.Files --version 1.1.0
NuGet\Install-Package Sion.Useful.Files -Version 1.1.0
<PackageReference Include="Sion.Useful.Files" Version="1.1.0" />
paket add Sion.Useful.Files --version 1.1.0
#r "nuget: Sion.Useful.Files, 1.1.0"
// Install Sion.Useful.Files as a Cake Addin #addin nuget:?package=Sion.Useful.Files&version=1.1.0 // Install Sion.Useful.Files as a Cake Tool #tool nuget:?package=Sion.Useful.Files&version=1.1.0
Sion.Useful.Files
NuGet package that provides useful file methods. Specifically, CSV file reading.
Sion.Useful.Files.Csv
public static IEnumerable<string[]> Read(string path, string delimiter = ",", bool hasHeader = false)
public static IEnumerable<RowType> Read<RowType>(string path, string delimiter = ",", bool hasHeader = false) where RowType : class
public static IEnumerable<RowType> Read<RowType>(string path, Func<string[], RowType> customMappingFunc, string delimiter = ",", bool hasHeader = false)
How to use:
First, download the Sion.Useful.Files NuGet package, then include this using statement at the top of the file:
using Sion.Useful.Files;
Csv
Reading a CSV file and mapping it to a custom class (automatically)
Things to note:
- The delimiter defaults to a comma, but can be customized
- The hasHeader defaults to false
- The automatic mapping currently only supports classes that use built-in property types
- The automatic mapping works best if there's a header row on the CSV file with column names that match the property names for the custom class (casing does matter)
- Empty column values are treated as null
- Column values null and "null" are also treated as null
- If using null, please mark which properties are nullable in the class definition
// Every row in our CSV file is a Student object
public class Student {
public long Id { get; set; }
public string FirstName { get; set; } = "";
public string? MiddleName { get; set; }
public string LastName { get; set; } = "";
public bool IsGraduateStudent { get; set; }
}
students.csv:
Id,FirstName,MiddleName,LastName,IsGraduateStudent
1,Landon,Jameson,Smith,false
2,Avery,Elizabeth,Davis,true
3,Ethan,null,Johnson,false
4,Mia,Grace,Rodriguez,false
5,Oliver,William,Brown,true
6,Aria,Rose,Hernandez,false
7,Caleb,Alexander,Lee,true
8,Lila,Madison,Turner,false
// Reading the CSV
IEnumerable<Student> students = Csv.Read<Student>("students.csv", hasHeader: true);
Reading a CSV file and mapping it to a custom class with custom mapping
If you find that the default automatic mapping is just not cutting it, there is a way to define a custom mapping for your object. The Read method is overloaded to take in a Func parameter. This Func is where you'll do your custom mapping.
This Func has one parameter, a string array representing a row in the CSV file. It must return your custom class.
// Reading the CSV with your custom mapping
IEnumerable<Student> students = Csv.Read(
"students.csv",
(string[] row) => {
return new Student() {
Id = Convert.ToInt64(row[0]),
FirstName = row[1],
MiddleName = row[2] == "null" || String.IsNullOrWhiteSpace(row[2]) ? null : row[2],
LastName = row[3],
IsGraduateStudent = Convert.ToBoolean(row[4])
};
},
hasHeader = true
);
Reading a CSV file and receiving raw string data
Note: Empty column values will return as an empty string ""
IEnumerable<string[]> rows = Csv.Read("students.csv", hasHeader: true);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Adding a new Read method that lets users provide their own mapping logic.