You can perform any validation required in the event AfterReadRecord. If you want to continue processing the rest of the file, if there is an error, you also need to set ErrorModeto SaveAndContinue. The following is a working example.
[DelimitedRecord("|")]
public class MyClass
{
public string Field1;
public string Field2;
public string Field3;
}
class Program
{
static void Main(string[] args)
{
var engine = new FileHelperEngine<MyClass>();
engine.AfterReadRecord += new FileHelpers.Events.AfterReadHandler<MyClass>(engine_AfterReadRecord);
engine.ErrorMode = ErrorMode.SaveAndContinue;
MyClass[] validRecords = engine.ReadString("Hello||World");
ErrorInfo[] errors = engine.ErrorManager.Errors;
Assert.AreEqual(1, engine.TotalRecords);
Assert.AreEqual(0, validRecords.Length);
Assert.AreEqual(1, engine.ErrorManager.ErrorCount);
Assert.That(errors[0].ExceptionInfo.Message == "Field2 is invalid");
}
static void engine_AfterReadRecord(EngineBase engine, FileHelpers.Events.AfterReadEventArgs<MyClass> e)
{
if (String.IsNullOrWhiteSpace(e.Record.Field1))
throw new Exception("Field1 is invalid");
if (String.IsNullOrWhiteSpace(e.Record.Field2))
throw new Exception("Field2 is invalid");
if (String.IsNullOrWhiteSpace(e.Record.Field3))
throw new Exception("Field3 is invalid");
}
}
source
share