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.
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