2016-08-31 5 views
0

私はコードの下に使用してCSVファイルを読み込むとfilehelper検証を呼び出していFilehelperは、各行のすべてのエラーを捕捉しない

[DelimitedRecord(",")] 
[IgnoreEmptyLines] 
public class CustomerClass 
{ 
    [FieldConverter(ConverterKind.Date, "MM/dd/yyyy")] 
    private DateTime EffectiveDate; 

    [FieldNotEmpty] 
    [FieldQuoted(QuoteMode.OptionalForRead)] 
    private string CustomerID; 
} 

以下のようfilehelperエンジンによって使用されるクラス内の2つのフィールドを有する

var engine = new DelimitedFileEngine<CustomerClass>; 
engine.Options.IgnoreFirstLines = 1; 
engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; 
List<CustomerClass> Result = engine.ReadFile(filepath).ToList(); 
ErrorInfo[] errorInfo = engine.ErrorManager.Errors; 

有効期限(日付形式が間違っている)と顧客ID(null値が渡された)の両方でcsvにエラーがある場合は、各行に1番目のエラーのみが取り込まれます。

どのようにすべての列でエラーをキャプチャし、最初のエラーが発生したときに停止しないのですか? 私はFilehelper 3.1.5を使用しています

おかげで、

+1

ライブラリーは、行の残りの部分が無効になる可能性があるため、解析できないため、解析を続行しません。次の行に進みます。 – MarcosMeli

答えて

0

私はあなたが、あなたはフィールドごとに確認することができCsvHelperを使用することをお勧め:コメントで述べた@MarcosMeliよう

using (TextReader reader = File.OpenText("Pasta1.csv")) 
    { 
     var csv = new CsvReader(reader); 
     csv.Configuration.Delimiter = ","; 

     while (csv.Read()) 
     { 
      CustomerClass record = new CustomerClass(); 
      record.CustomerID = (string)csv.GetField(typeof(string), "CustomerID"); 
      if (string.IsNullOrEmpty(record.CustomerID)) 
      { 
       //CustomerID is null 
      } 

      if (!csv.TryGetField<DateTime>("EffectiveDate", out record.EffectiveDate)) 
      { 
       //EffectiveDate incorrect 
      } 
     } 
    } 
0

ライブラリでエラーが検出された場合、残りの行が無効になる可能性があるため、 の解析が続行されませんXT ライン

あなたが本当にすべてのエラーを報告する必要がある場合、あなたはAfterReadRecordイベントを使用して は手で処理の多くを行う必要があります。

まず、すべてのフィールドをstringに変更してエラーがないようにする必要があります。

[DelimitedRecord(",")] 
[IgnoreEmptyLines] 
public class CustomerClass : 
{ 
    public string EffectiveDate; 

    [FieldQuoted(QuoteMode.OptionalForRead)] 
    public string CustomerID; 
} 

イベントを追加してそこですべての検証を処理する必要があります。何かのように:

関連する問題