/// <summary> /// Reads a record from the CSV file. /// </summary> /// <returns>A <see cref="List{String}" /> of fields for the record read. /// If there are no more records, null is returned.</returns> public virtual string[] Read() { CheckDisposed(); try { var row = ReadLine(); if (configuration.DetectColumnCountChanges && row != null) { if (columnCount > 0 && (columnCount != row.Length || #if NET_2_0 EnumerableHelper.Any(row, field => field == null) #else row.Any(field => field == null) #endif )) { throw ExceptionHelper.GetReaderException <CsvBadDataException>("An inconsistent number of columns has been detected.", null, this, null, null, null, null); } columnCount = row.Length; } return(row); } catch (CsvHelperException) { // We threw it, so let it go. throw; } catch (Exception ex) { throw ExceptionHelper.GetReaderException <CsvParserException>("A parsing error occurred.", ex, this, null, null, null, null); } }
/// <summary> /// Gets all the records in the CSV file and /// converts each to <see cref="Type"/> T. The Read method /// should not be used when using this. /// </summary> /// <param name="type">The <see cref="Type"/> of the record.</param> /// <returns>An <see cref="IList{Object}" /> of records.</returns> public virtual IEnumerable <object> GetRecords(Type type) { CheckDisposed(); // Don't need to check if it's been read // since we're doing the reading ourselves. while (Read()) { object record; try { record = GetReadRecordFunc(type)(this); } catch (CsvReaderException) { // We threw the exception, so let it go. throw; } catch (Exception ex) { throw ExceptionHelper.GetReaderException <CsvReaderException>("An error occurred reading the record.", ex, parser, type, namedIndexes, currentIndex, currentRecord); } yield return(record); } }
/// <summary> /// Gets the record. /// </summary> /// <param name="type">The <see cref="Type"/> of the record.</param> /// <returns>The record.</returns> public virtual object GetRecord(Type type) { CheckDisposed(); CheckHasBeenRead(); object record; try { record = GetReadRecordFunc(type)(this); } catch (CsvReaderException) { // We threw the exception, so let it go. throw; } catch (Exception ex) { throw ExceptionHelper.GetReaderException <CsvReaderException>("An error occurred reading the record.", ex, parser, type, namedIndexes, currentIndex, currentRecord); } return(record); }
/// <summary> /// Gets the record converted into <see cref="Type"/> T. /// </summary> /// <typeparam name="T">The <see cref="Type"/> of the record.</typeparam> /// <returns>The record converted to <see cref="Type"/> T.</returns> public virtual T GetRecord <T>() where T : class { CheckDisposed(); CheckHasBeenRead(); T record; try { record = GetReadRecordFunc <T>()(this); } catch (CsvReaderException) { // We threw the exception, so let it go. throw; } catch (Exception ex) { throw ExceptionHelper.GetReaderException <CsvReaderException>("An error occurred reading the record.", ex, parser, typeof(T), namedIndexes, currentIndex, currentRecord); } return(record); }