Click button to edit
QuickStart: Write Delimited File
Example of how to write a Delimited File
To write an output file (separated by a "|"):
Output.txt
1|AIRG|01052009|82.43
2|JSYV|02052009|12.22
RecordClass.cs
/// <summary>
/// Layout for a file delimited by |
/// </summary>
[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 write the file:
Example.cs
var engine = new FileHelperEngine<Orders>();
var orders = new List<Orders>();
orders.Add(new Orders() {
OrderID = 1,
CustomerID = "AIRG",
Freight = 82.43M,
OrderDate = new DateTime(2009, 05, 01)
});
orders.Add(new Orders() {
OrderID = 2,
CustomerID = "JSYV",
Freight = 12.22M,
OrderDate = new DateTime(2009, 05, 02)
});
engine.WriteFile("Output.Txt", orders);
The classes you use could come from anywhere: LINQ to Entities, SQL database reads, or in this case, classes created within an application.
Console
1|AIRG|01052009|82.43
2|JSYV|02052009|12.22