Skip to content

converting datetime with layout #51

Description

@pelife

Idid not find any property to instruct how to convert dates in my flat file to a property of time DateTime and when I tried to read the file, an exception was thrown converting 20170712 to type DateTime.

public class BDIHeader
    {
        public int Tipo { get; set; }
        public string NomeArquivo { get; set; }
        public string Origem { get; set; }
        public int Destino { get; set; }
        public DateTime DataGeracao { get; set; }
        public DateTime DataPregao { get; set; }
        public string HoraMinuto { get; set; }
    }
public sealed class BDIHeaderLayout : FixedLayout<BDIHeader>
    {
        public BDIHeaderLayout()
        {
            this.WithMember(x => x.Tipo, c => c.WithLength(2))
                .WithMember(x => x.NomeArquivo, c => c.WithLength(8))
                .WithMember(x => x.Origem, c => c.WithLength(8))
                .WithMember(x => x.Destino, c => c.WithLength(4))
                .WithMember(x => x.DataGeracao, c => c.WithLength(8))
                .WithMember(x => x.DataPregao, c => c.WithLength(8))
                .WithMember(x => x.HoraMinuto, c => c.WithLength(4));
        }
    }
public void Read()
        {
            //
            var factory = new FixedLengthFileEngineFactory();
            using (var stream = new FileInfo(enderecoArquivoBDI).Open(FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                // If using attribute mapping, pass an array of record types
                // rather than layout instances
                var layouts = new ILayoutDescriptor<IFixedFieldSettingsContainer>[]
                {
                    new BDIHeaderLayout(),new BDIIndiceLayout()

                };
                
                var flatFile = factory.GetEngine(layouts,
                    line =>
                    {
                        // For each line, return the proper record type.
                        // The mapping for this line will be loaded based on that type.
                        // In this simple example, the first character determines the
                        // record type.
                        if (String.IsNullOrEmpty(line) || line.Length < 1) return null;
                        switch (line.Substring(0, 2))
                        {
                            case "00":
                                return typeof(BDIHeader);
                            //case "01":
                            //    return typeof(BDIIndice);
                                //case "02":
                                //    return typeof(BDINegociosPapelLayout);
                                //case "99":
                                //    return typeof(BDITrailerLayout);
                        }
                        return null;
                    });

                flatFile.Read(stream);

                var header = flatFile.GetRecords<BDIHeader>().FirstOrDefault();
                //var indices = flatFile.GetRecords<BDIIndice>().ToList();
                //var negocios = flatFile.GetRecords<BDINegociosPapelLayout>();
                //var trailer = flatFile.GetRecords<BDITrailer>().FirstOrDefault();
            }
        }

line being read
00BDIN9999BOVESPA 999920170713201707131807

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions