如何配置CsvHelper跳过MissingFieldFound行

时间:2018-10-01 11:12:00

标签: c# csv csvhelper

var config = new Configuration
{
    MissingFieldFound = (rows, fieldIndex, readingContext) =>
        Log.Warn($"Missing Field Found at line {readingContext.Row}\r\n" +
                $"Field at index {fieldIndex} does not exist\r\n" +
                $"Raw record: {readingContext.RawRecord}"),
}
发现缺少字段时会调用

MissingFieldFound属性,但不会影响结果。

我想知道是否可以将CsvHelper配置为跳过缺少字段的行。

4 个答案:

答案 0 :(得分:2)

您的操作方式没有错,这是一个显示完整示例的mcve

var good = new List<Test>();
var bad = new List<string>();

using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
using (var reader = new StreamReader(stream))
using (var csv = new CsvReader(reader))
{
    writer.WriteLine("FirstName,LastName");
    writer.WriteLine("\"Jon\"hn\"\",\"Doe\"");
    writer.WriteLine("\"JaneDoe\"");
    writer.WriteLine("\"Jane\",\"Doe\"");
    writer.Flush();
    stream.Position = 0;

    var isRecordBad = false;

    csv.Configuration.BadDataFound = context =>
    {
        isRecordBad = true;
        bad.Add(context.RawRecord);
    };

    csv.Configuration.MissingFieldFound = (headerNames, index, context) =>
    {
        isRecordBad = true;
        bad.Add(context.RawRecord);
    };

    while (csv.Read())
    {
        var record = csv.GetRecord<Test>();
        if (!isRecordBad)
        {
            good.Add(record);
        }

        isRecordBad = false;
    }
}

good.Dump();
bad.Dump();

答案 1 :(得分:1)

跳过MissingFieldFound行的另一种方法是使用ShouldSkipRecord并将标头记录长度与行记录长度进行比较。

// Load header record if you haven't already (CsvDataReader loads it automatically).
csv.Read();
csv.ReadHeader();

// Then do this.
var expectedRecordLength = csv.Context.HeaderRecord.Length;
csv.Configuration.ShouldSkipRecord = rowRecord => rowRecord.Length != expectedRecordLength;

如果您不控制阅读代码(例如,将CsvDataReaderSqlBulkCopy一起使用时,这特别有用)。

答案 2 :(得分:1)

使用我拥有的新版本的 csvhelper (24.0.1),下面的代码将用于设置 MissingFieldFound

Dim textReader As TextReader = File.OpenText(filename)

Dim config = New CsvHelper.Configuration.CsvConfiguration(System.Globalization.CultureInfo.InvariantCulture)

        config.Delimiter = ","
        config.MissingFieldFound = Nothing
        config.TrimOptions = True
        config.HeaderValidated = Nothing

Dim csv = New CsvReader(textReader, config)

答案 3 :(得分:0)

您还可以使用属性配置缺失的字段

[可选]

公共字符串字段{get;设置;}

[忽略]

public int 归档 {get;设置;}