c# – CsvHelper无法正确解析

我有以下CSV.

Org Defined ID,Username,FirstName,LastName,Attempt #,Attempt Start,Attempt End,Section #,Q #,Q Type,Q Title,Q Text,Bonus?,Difficulty,Answer,Answer Match,Score,Out Of
,testomalley,Test,O'Malley,1,2/3/2016 15:24,2/3/2016 15:28,,1,LA,Q(1) 1- 5 Part 1,"Scenario 1 for Questions 1 through 5. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.  Part 1 - Sed ut perspiciatis unde omnis iste natus error sit voluptatem.",FALSE,1,Here is my answer o grader. Isn't it brilliant?,,0,2

现在这是我的对象我正在尝试将其转换为:

public class Exam
{
    public int? OD_ID { get; set; }
    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int AttemptNo { get; set; }
    public DateTime AttemptStart { get; set; }
    public DateTime AttemptEnd { get; set; }
    public int? SectionNo { get; set; }
    public int QuestionNo { get; set; }
    public string QuestionType { get; set; }
    public string QuestionTitle { get; set; }
    public string Questiontext { get; set; }
    public string Bonus { get; set; }
    public int Difficulty { get; set; }
    public string Answer { get; set; }
    public string AnswerMatch { get; set; }
    public int Score { get; set; }
    public int OutOf { get; set; }
}

我定义了这张地图:

public sealed class ExamMap : CsvHelper.Configuration.CsvClassMap<Exam>
{
    public ExamMap()
    {
        Map(m => m.OD_ID).Name("Org Defined ID");
        Map(m => m.Username).Name("Username");
        Map(m => m.FirstName).Name("FirstName");
        Map(m => m.LastName).Name("LastName");
        Map(m => m.AttemptNo).Name("Attempt #");
        Map(m => m.AttemptStart).Name("Attempt Start");
        Map(m => m.AttemptEnd).Name("Attempt End");
        Map(m => m.SectionNo).Name("Section #");
        Map(m => m.QuestionNo).Name("Q #");
        Map(m => m.QuestionType).Name("Q Type");
        Map(m => m.QuestionTitle).Name("Q Title");
        Map(m => m.Questiontext).Name("Q Text");
        Map(m => m.Bonus).Name("Bonus?").TypeConverterOption(true, "TRUE").TypeConverterOption(false, "FALSE");
        Map(m => m.Difficulty).Name("Difficulty");
        Map(m => m.Answer).Name("Answer");
        Map(m => m.AnswerMatch).Name("Answer Match");
        Map(m => m.Score).Name("Score");
        Map(m => m.OutOf).Name("Out Of");
    }
}

和我的DateTime字段的TypeConversion选项:

var options = new CsvHelper.TypeConversion.TypeConverterOptions
{
    //        2/3/2016 15:24
    Format = "MM/dd/yyyy HH:nn", // also tried "g" with no success.
};

这是解析CSV文件的代码:

using (StreamReader sr = new StreamReader(fileName))
{
    using (CsvReader csvread = new CsvReader(sr))
    {
        csvread.Configuration.WillThrowOnMissingField = false;
        csvread.Configuration.HasHeaderRecord = true;
        CsvHelper.TypeConversion.TypeConverterOptionsFactory.AddOptions<DateTime>(options);

        List<Exam> Exams = csvread.GetRecords<Exam>().ToList();
        foreach (var rec in Exams) 
        {
          // Each record will be fetched and printed on the screen
          // checking values of rec here...
        }
    }
}

以下是它的解析方式:
《c# – CsvHelper无法正确解析》

为什么它没有正确解析DateTime字段,为什么这些属性没有被填充?

QuestionNo
问题类型
QuestionTitle
Questiontext
AnswerMatch
AttemptNo
在……之外

最佳答案 您可以像这样添加DateTime解析方法(您甚至可以立即设置断点并修改选项):

Map(t => t.AttemptStart).Name("Attempt Start")
    .ConvertUsing(new Func<CsvHelper.ICsvReaderRow, DateTime>(r =>
        {
            DateTime dateTimeValue;
            if (DateTime.TryParse(r["Attempt Start"], null,
                System.Globalization.DateTimeStyles.AllowWhiteSpaces, out dateTimeValue))
            {
                return dateTimeValue;
            }

            return default(DateTime);
        }));

缺少的字段可能是基于DateTime解析问题的错误.一见钟情看起来很正确.

点赞