FileHelpers - Before/After Read Event Handling

  • 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. Events And Notification
  3. Before/After Read Event Handling
Previous: INotifyWrite Interface
Next: Before/After Write Event Handling
Click button to edit
Events And Notification: Before/After Read Event Handling
Show how to implement read events

Reads report.inp and skips all the records that are not detail records using a simple criteria

report.inp
-----------------------------------------------------
              XXX Enterprise
-----------------------------------------------------
10249   TOMSP  05071996      11.61
10250   HANAR  08071996      65.83
10251   VICTE  08071996       0.00
                                             Page 1
-----------------------------------------------------
                YYY Enterprise
-----------------------------------------------------
10269   TOMSP  05071996      11.61
10230   HANAR  08071996       0.00
10151   VICTE  08071996      41.34
Report layout.cs
[FixedLengthRecord(FixedMode.AllowVariableLength)]
[IgnoreEmptyLines]
public class OrdersFixed
{
    [FieldFixedLength(7)]
    public int OrderID;

    [FieldFixedLength(8)]
    public string CustomerID;

    [FieldFixedLength(8)]
    public DateTime OrderDate;

    [FieldFixedLength(11)]
    public decimal Freight;
}
RunEngine.cs
public override void Run()
{
    var engine = new FileHelperEngine<OrdersFixed>();
    engine.BeforeReadRecord += BeforeEvent;
    engine.AfterReadRecord += AfterEvent;

    var result = engine.ReadFile("report.inp");

    foreach (var value in result)
        Console.WriteLine("Customer: {0} Freight: {1}", value.CustomerID, value.Freight);

}

private void BeforeEvent(EngineBase engine, BeforeReadEventArgs<OrdersFixed> e)
{
    if (e.RecordLine.StartsWith(" ") ||
        e.RecordLine.StartsWith("-"))
        e.SkipThisRecord = true;

    //  Sometimes changing the record line can be useful, for example to correct for
    //  a bad data layout.  Here is an example of this, commented out for this example

    //if (e.RecordLine.StartsWith(" "))
    //   e.RecordLine = "Be careful!";
}


private void AfterEvent(EngineBase engine, AfterReadEventArgs<OrdersFixed> e)
{
    //  we want to drop all records with no freight
    if (e.Record.Freight == 0)
        e.SkipThisRecord = true;
}
Console
Customer:  TOMSP   Freight: 11,61
Customer:  HANAR   Freight: 65,83
Customer:  TOMSP   Freight: 11,61
Customer:  VICTE   Freight: 41,34

ImportantYou can use lambda expressions instead of event methods, for example:

RunEngineLambda.cs
public void RunLambda()
{
    var engine = new FileHelperEngine<OrdersFixed>();
    engine.BeforeReadRecord += (eng, e) => {
        if (e.RecordLine.StartsWith (" ") ||
            e.RecordLine.StartsWith ("-"))
            e.SkipThisRecord = true;
    };
    engine.AfterReadRecord +=  (eng, e) => {
        if (e.Record.Freight == 0)
            e.SkipThisRecord = true;
    };

    var result = engine.ReadFile("report.inp");

    foreach (var value in result)
        Console.WriteLine("Customer: {0} Freight: {1}", value.CustomerID, value.Freight);

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