FileHelpers - Read Delimited File

  • Contact / Feedback
  • Introduction
  • Download
  • Quick Start
  • Must Read
  • Examples
    • QuickStart
      • Read Delimited File
      • Write Delimited File
      • Read Fixed File
      • Write Fixed File
      • Read or Write Record by Record
      • Autoproperties
    • Missing Values
      • Handle Missing Values with Nullable
      • Handle Missing Values With FieldNullValue
    • Attributes
      • FieldTrim
      • FieldOrder
      • FixedLengthRecord FixedMode.AllowLessChars
    • Converters
      • Enum Converter
      • Custom Converter
    • Events And Notification
      • INotifyRead Interface
      • INotifyWrite Interface
      • Before/After Read Event Handling
      • Before/After Write Event Handling
    • ErrorHandling
      • ErrorMode.ThrowException
      • ErrorMode.IgnoreAndContinue
      • ErrorMode SaveAndContinue
    • Advanced
      • Dynamic Engine Options
      • Multiple Delimiters
      • Multi Record Processing
      • Smart Format Detector
    • Sorting
      • Sort Big File with Record Class
      • Sort Big File without Record Class 1
      • Sort Big File without Record Class 2
    • MasterDetail
      • Master Detail Custom Selector
      • Master Detail Common Selector
  • Credits
  • Invite us a beer
  • Contribute
  • Source Code
  • Api Docs (Sandcastle)
  1. Examples
  2. QuickStart
  3. Read Delimited File
Next: Write Delimited File
Click button to edit
QuickStart: Read Delimited File
How to read a Delimited File

If you have a source file like this, separated by a |:

Input.txt
10248|VINET|04071996|32.38
10249|TOMSP|05071996|11.61
10250|HANAS|08071996|65.83
10251|VICTE|08071996|41.34

You first declare a Record Mapping Class:

RecordClass.cs
[DelimitedRecord("|")]
public class Orders
{
    public int OrderID;

    public string CustomerID;

    [FieldConverter(ConverterKind.Date, "ddMMyyyy")]
    public DateTime OrderDate;

    [FieldConverter(ConverterKind.Decimal, ".")] // The decimal separator is .
    public decimal Freight;
}

Instantiate a FileHelperEngine and read or write files:

Example.cs
var engine = new FileHelperEngine<Orders>();
var records = engine.ReadFile("Input.txt");

foreach (var record in records)
{
    Console.WriteLine(record.CustomerID);
    Console.WriteLine(record.OrderDate.ToString("dd/MM/yyyy"));
    Console.WriteLine(record.Freight);
}

Now you have an Orders array named res where every item in the array is an Order object. If you want to access one of the fields let the Visual Studio IntelliSense bring up the field names for you.

Visual studio intellisense
Console
VINET
04/07/1996
32,38
TOMSP
05/07/1996
11,61
HANAS
08/07/1996
65,83
VICTE
08/07/1996
41,34
Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs