FileHelpers - Write 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. Write Delimited File
Previous: Read Delimited File
Next: Read Fixed File
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

Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs