简体   繁体   中英

C# - FileHelpers: Bad computing of DateTime variable

I have this sample CSV file:

Filip    Malýn        Male  1218-02-1994
Božena   Němcová      Female1804-02-1820
Jan      Žižka        Male  0719-09-1360
Che      Guevara      Male  2714-06-1928
AntoinedeSaint-ExupéryMale  1529-06-1900

I load it in a code by this function:

FileHelperEngine<T>().ReadFile(fileName);

But it ends up with this error:

FileHelpers.BadUsageException: 'The string '18-02-1994' (length 10) at line 1 has less chars than the defined for BirthDate (11). You can use the [FixedLengthRecord(FixedMode.AllowLessChars)] to avoid this problem.'

And if I add [FixedLengthRecord(FixedMode.AllowLessChars)] to the code it ends up with this error:

FileHelpers.ConvertException: 'Error Converting 'al' to type: 'Int32'. '

This is a class I use:

using System;
using FileHelpers;

namespace ImportExport.Mapping.FixedLength
{
    [FixedLengthRecord(FixedMode.AllowLessChars)]
    public class Person
    {
        [FieldFixedLength(9)]
        public String Name;

        [FieldFixedLength(13)]
        public String Surname;

        [FieldFixedLength(6)]
        public String Gender;

        [FieldFixedLength(2)]
        public Int32 OrderNum;

        [FieldFixedLength(11)]
        [FieldConverter(ConverterKind.Date, "dd-MM-yyyy")]
        public DateTime BirthDate;
    }
}

I have been couting it so many times and tried a lot of version but with no success. What's wrong?

Thanks all. The solution is adding an info about encoding of the file. In my case changing FileHelperEngine<T>().ReadFile(fileName); to FileHelperEngine<T>(Encoding.UTF8).ReadFile(fileName); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM