FileHelpers - INotifyRead Interface

  • 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. INotifyRead Interface
Previous: Custom Converter
Next: INotifyWrite Interface
Click button to edit
Events And Notification: INotifyRead Interface
Get Before/After Read events with the INotifyRead interface

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
    :INotifyRead
{
    [FieldFixedLength(7)]
    public int OrderID;

    [FieldFixedLength(8)]
    public string CustomerID;

    [FieldFixedLength(8)]
    public DateTime OrderDate;

    [FieldFixedLength(11)]
    public decimal Freight;


    public void BeforeRead(BeforeReadEventArgs e)
    {
        if (e.RecordLine.StartsWith(" ") ||
           e.RecordLine.StartsWith("-"))
            e.SkipThisRecord = true;
    }

    public void AfterRead(AfterReadEventArgs e)
    {   
        //  we want to drop all records with no freight
        if (Freight == 0)
            e.SkipThisRecord = true;

    }

}
RunEngine.cs
var engine = new FileHelperEngine<OrdersFixed>();
var result = engine.ReadFile("report.inp");

foreach (var value in result)
    Console.WriteLine("Customer: {0} Freight: {1}", value.CustomerID, value.Freight);
Console
Customer:  TOMSP   Freight: 11,61
Customer:  HANAR   Freight: 65,83
Customer:  TOMSP   Freight: 11,61
Customer:  VICTE   Freight: 41,34
Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs