public void PushValues(TIn input, Action <TOut> push) { using (var sr = new StreamReader(_args.DataStreamSelector(input))) { if (_args.Mapping.HasColumnHeader) { int index = 0; LineSerializer <TParsed> lineSerializer = null; while (!sr.EndOfStream) { string line = sr.ReadLine(); if (index == _args.Mapping.FirstLinesToIgnore) { lineSerializer = _args.Mapping.GetSerializer(line); } else if (index > _args.Mapping.FirstLinesToIgnore && !string.IsNullOrWhiteSpace(line)) { TParsed parsed; try { parsed = lineSerializer.Deserialize(line); } catch (Exception ex) { throw new FlatFileLineDeserializeException(index, ex); } push(_args.ResultSelector(input, parsed)); } index++; } } else { int index = 0; LineSerializer <TParsed> lineSerializer = _args.Mapping.GetSerializer(); while (!sr.EndOfStream) { string line = sr.ReadLine(); if (index >= _args.Mapping.FirstLinesToIgnore && !string.IsNullOrWhiteSpace(line)) { TParsed parsed; try { parsed = lineSerializer.Deserialize(line); } catch (Exception ex) { throw new FlatFileLineDeserializeException(index, ex); } push(_args.ResultSelector(input, parsed)); } index++; } } } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var example = new Example() { DocumentType = 0, Operation = 1, OperationName = "OPNAME", ServiceCode = 2, ServiceName = "SERVNAME", ActionCode = "3", FunctionCode = "7", CheckDigit = "4", CompanyName = "Example LTD", IssuerCode = "123", IssuerName = "Issuer LTD", CreationDate = DateTime.Now, SequencialCode = 1, }; var opt = new SerializationOptions() { DateTimeFormat = "ddMMyy" }; LineSerializer line = new LineSerializer(opt); using var ms = new MemoryStream(); using var sw = new StreamWriter(ms); line.Serialize(example, sw); var text = Encoding.UTF8.GetString(ms.ToArray()); Console.WriteLine(text); // Convert back var example2 = line.Deserialize <Example>(text.Replace("\r\n", ""), out _); }