Click button to edit
ErrorHandling: ErrorMode SaveAndContinue
Read the file saving bad records
Input.txt
ALFKI|Alfreds Futterkiste|Maria Anders|SalesRepresentative
ANATR|Ana Trujillo Emparedados y helados|Ana Trujillo|NotInEnum
FRANR|France restauration|Carine Schmitt|MarketingManager
ANTON|Antonio Moreno Taquería|Antonio Moreno|Owner
Customers with Enum.cs
[DelimitedRecord("|")]
public class Customer
{
public string CustomerID;
public string CompanyName;
public string ContactName;
public CustomerTitle ContactTitle;
}
public enum CustomerTitle
{
Owner,
SalesRepresentative,
MarketingManager
}
RunEngine.cs
public override void Run()
{
var engine = new DelimitedFileEngine<Customer>();
// Switch error mode on
engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;
// This fails with not in enumeration error
var customers = engine.ReadFile("Input.txt");
if (engine.ErrorManager.HasErrors)
engine.ErrorManager.SaveErrors("errors.out");
LoadErrors();
}
private void LoadErrors()
{
// sometime later you can read it back using:
ErrorInfo[] errors = ErrorManager.LoadErrors("errors.out");
// This will display error from line 2 of the file.
foreach (var err in errors) {
Console.WriteLine();
Console.WriteLine("Error on Line number: {0}", err.LineNumber);
Console.WriteLine("Record causing the problem: {0}", err.RecordString);
Console.WriteLine("Complete exception information: {0}", err.ExceptionInfo.ToString());
}
}
Errors.out
FileHelpers - Errors Saved at jueves, 26 de noviembre de 2015 04:07:27 p.m.
LineNumber | LineString |ErrorDescription
2|"ANATR|Ana Trujillo Emparedados y helados|Ana Trujillo|NotInEnum"|In the field 'ContactTitle': Error Converting 'NotInEnum' to type: 'CustomerTitle'. The value NotInEnum is not present in the Enum.
Console
Error on Line number: 2
Record causing the problem: ANATR|Ana Trujillo Emparedados y helados|Ana Trujillo|NotInEnum
Complete exception information: System.Exception: In the field 'ContactTitle': Error Converting 'NotInEnum' to type: 'CustomerTitle'. The value NotInEnum is not present in the Enum.