Tuvia
Tuvia

Reputation: 21

Is there a way to retrieve the entire line that the FileHelpers Engine is parsing

We are using FileHelpers to parse a text file into numerous entities, based on the first character in the line. Each entity is then stored in a particular database table. We would also like to store each input string, as a whole, in addition to the parsed fields.

Is there a way to capture the input line, before it is parsed into the individual fields of the entity?

Upvotes: 2

Views: 118

Answers (1)

Marcos Meli
Marcos Meli

Reputation: 3506

You can use events to get the full line, BeforeReadRecord or AfterReadRecord have an argument that contains the property RecordLine

Here is an example: https://www.filehelpers.net/example/EventsAndNotification/ReadEvents/

[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;
}

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)
{
     Console.Write(e.RecordLine)
}


private void AfterEvent(EngineBase engine, AfterReadEventArgs<OrdersFixed> e)
{
     Console.Write(e.RecordLine)
}

Upvotes: 1

Related Questions