Reading TSV files

May 6, 2010 at 11:53 AM

Kent,

Great little utility, most useful CSV reader I've come across.

Is there an example for reading TSV (tab delim.) files?  I've tried changing the ValueDelimiter to tab, but it doesn't seem to be working.  I'm not sure if it is the file I'm working with or if I'm coding it incorrectly.  Can you provide an example?

Thanks,

Marc

Coordinator
May 10, 2010 at 1:58 PM
Edited May 10, 2010 at 5:57 PM
Hi Marc,

Here is a simple example:

using System;
using Kent.Boogaart.KBCsv;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var csvReader = new CsvReader("Data.txt"))
            {
                csvReader.ValueSeparator = '\t';
                csvReader.ReadHeaderRecord();

                while (csvReader.HasMoreRecords)
                {
                    var record = csvReader.ReadDataRecord();
                    Console.WriteLine("Name: {0}, Age: {1}, Nationality: {2}",
                        record["Name"],
                        record["Age"],
                        record["Nationality"]);
                }
            }

            Console.ReadKey();
        }
    }
}
The data file looks as you'd expect. Something like this, where the whitespace is a tab character:
Name	Age	Nationality
Kent	30	Australian
Belinda	31	Australian/British
Tempany	5	Australian
Does that clear things up for you?

Best,
Kent