Exemplo n.º 1
0
        private IDataItem ReadNext(object taskState)
        {
            var readOutput = (ReadOutputByRef)taskState;

            try
            {
                if (file == null)
                {
                    file      = File.OpenText(configuration.FileName);
                    csvReader = new CsvReader(file);
                }

                if (!csvReader.Read())
                {
                    return(null);
                }

                // Unfortunatelly there is no support for mapping to a Dictionary yet.
                // Use dynamic workaround https://github.com/JoshClose/CsvHelper/issues/187
                var record = csvReader.GetRecord <dynamic>() as IDictionary <string, object>;

                return(NestedDataItem.Create(record, configuration.NestingSeparator));
            }
            finally
            {
                if (csvReader != null)
                {
                    // If it fails on the first read - it will throw an exception from Row property
                    try { rowNumber = csvReader.Row; } catch { }
                }

                readOutput.DataItemId = String.Format(CultureInfo.InvariantCulture,
                                                      Resources.DataItemIdFormat, configuration.FileName, rowNumber);
            }
        }
Exemplo n.º 2
0
        public override async Task <IDataItem> ReadNextAsync(ReadOutputByRef readOutput, CancellationToken cancellationToken)
        {
            readOutput.DataItemId = String.Format(CultureInfo.InvariantCulture, Resources.DataItemIdFormat, ++rowNumber);

            while (!await dataReader.ReadAsync(cancellationToken))
            {
                if (!await dataReader.NextResultAsync())
                {
                    return(null);
                }
            }

            var dataItem = NestedDataItem.Create(Configuration.NestingSeparator);

            for (var fieldIndex = 0; fieldIndex < dataReader.FieldCount; ++fieldIndex)
            {
                dataItem.AddProperty(
                    dataReader.GetName(fieldIndex),
                    dataReader.GetValue(fieldIndex));
            }

            return(dataItem);
        }
Exemplo n.º 3
0
        private IDataItem ReadNext()
        {
            var values = reader.Read();

            if (values == null)
            {
                return(null);
            }

            if (values.Count != header.Count)
            {
                throw Errors.InvalidNumberOfColumns(values.Count, header.Count);
            }

            var dataItem = NestedDataItem.Create(configuration.NestingSeparator);

            for (var index = 0; index < header.Count; ++index)
            {
                dataItem.AddProperty(header[index], values[index]);
            }

            return(dataItem);
        }