C# - FileHelpers:日期时间变量的错误计算

C# - FileHelpers: Bad computing of DateTime variable

我有这个示例 CSV 文件:

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

我通过这个函数将它加载到代码中:

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

但它以这个错误结束:

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

如果我将 [FixedLengthRecord(FixedMode.AllowLessChars)] 添加到代码中,它最终会出现此错误:

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

这是一个class我用:

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

我已经计算了很多次,尝试了很多版本都没有成功。怎么了?

谢谢大家。解决方案是添加有关文件编码的信息。在我的例子中,将 FileHelperEngine<T>().ReadFile(fileName); 更改为 FileHelperEngine<T>(Encoding.UTF8).ReadFile(fileName);.