RecycleBin.TextTables

Reads and writes a data table in text format such as CSV, TSV, and space-delimited text. Also provides a way to bind each row to user defined class, so that user can easily create instances from a text table.


Keywords
table, CSV, TSV, space-delimited, LINQ
License
BSD-3-Clause
Install
Install-Package RecycleBin.TextTables -Version 1.3.1

Documentation

TextTables

This is a .NET Framework library providing classes handle plain-text tables like CSV. Refer to the Wiki for details.

Features

  • Easy to Use:
using (var csv = new CsvReader(@"X:\Path\To\File.csv"))
{
   while (csv.MoveNext())
   {
      var record = csv.Current;
      Console.WriteLine("First Column: {0}; Number of Fields: {1}", record[0], record.FieldCount);
   }
}
  • Dynamic Mapping:

Suppose we have a space-separated table as the following:

Id     Name    Birthday
 1    Alice  2001-02-14
 2      Bob  1997-06-05   
 3  Charlie  1999-12-15
 4     Dave  2005-10-30

So we can define a class like this:

public class Person
{
   [Column("Id")]
   public long ID { get; set; }

   [Column("Name")]
   public string Name { get; set; }

   [Column("Birthday")]
   public DateTime Birthday { get; set; }
}

Now, we can read the text file.

using (var table = new SpaceSeparatedTableReader(@"X:\Path\TO\Table.txt"))
{
   // Notify the reader that the first line is the header.
   table.HandleHeaderRow();

   // LINQ is available.
   var query = from person in table.ReadToEnd<Person>()
               where person.Birthday < new DateTime(2000, 01, 01)
               select person;
   foreach (var person in query)
   {
      Console.WriteLine(person.Name);
   }
}