CsvHelper does not correctly understand

I have the following 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

Now here is my object that I am trying to convert:

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; }
}

I defined this mapping:

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");
    }
}

and this TypeConversion parameter for the DateTime field:

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

This is the code to parse the CSV file:

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...
        }
    }
}

Here's how he understands: Values

Why does this not correctly handle DateTime fields and why are these properties not populated?

QuestionNo
QuestionType
QuestionTitle
Questiontext
AnswerMatch
AttemptNo
OUTOF

+4
source share
2 answers

You can add a method for parsing DateTime (you can even set a breakpoint now and change the parameters):

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. .

+1

, , . , , .

csv.Configuration.RegisterClassMap<ExamMap>();
csv.GetRecords<Exam>().ToList().Dump();

, , , .

CsvHelper . , , .

TypeConverterOptions

linqpad.

void Main()
{
    using (var stream = new MemoryStream())
    using (var reader = new StreamReader(stream))
    using (var writer = new StreamWriter(stream))
    using (var csv = new CsvReader(reader))
    {
        writer.WriteLine("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");
        writer.WriteLine(", 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");
        writer.Flush();
        stream.Position = 0;

        csv.Configuration.RegisterClassMap<ExamMap>();
        csv.GetRecords<Exam>().ToList().Dump();
    }
}

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");
    }
}
0

All Articles