private void WriteData <T>( IEnumerable <T> values, string fileName, TextWriter stream, CsvFileDescription fileDescription) { FieldMapper <T> fm = new FieldMapper <T>(fileDescription, fileName, true); CsvStream cs = new CsvStream(null, stream, fileDescription.SeparatorChar); List <string> row = new List <string>(); if (fileDescription.FirstLineHasColumnNames) { fm.WriteNames(ref row); cs.WriteRow(row, fileDescription.QuoteAllFields); } // ----- foreach (T obj in values) { // Convert obj to row fm.WriteObject(obj, ref row); cs.WriteRow(row, fileDescription.QuoteAllFields); } }
private IEnumerable <T> ReadData <T>( string fileName, StreamReader stream, CsvFileDescription fileDescription) where T : class, ICsvRow, new() { bool readingRawDataRows = typeof(IDataRow).IsAssignableFrom(typeof(T)); FieldMapper_Reading <T> fm = null; if (!readingRawDataRows) { fm = new FieldMapper_Reading <T>(fileDescription, fileName, false); } bool readingFile = !string.IsNullOrEmpty(fileName); if (readingFile) { stream = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read)); } else { if ((stream == null) || (!stream.BaseStream.CanSeek)) { throw new BadStreamException(); } stream.BaseStream.Seek(0, SeekOrigin.Begin); } CsvStream cs = new CsvStream(stream, null, fileDescription.SeparatorChar); IDataRow row = null; if (readingRawDataRows) { row = new T() as IDataRow; } else { row = new DataRow(); } AggregatedException ae = new AggregatedException(typeof(T).ToString(), fileName, fileDescription.MaximumNbrExceptions); try { bool firstRow = true; while (cs.ReadRow(ref row)) { if ((row.Count == 1) && ((row[0].Value == null) || (string.IsNullOrEmpty(row[0].Value.Trim())))) { continue; } if (firstRow && fileDescription.FirstLineHasColumnNames) { if (!readingRawDataRows) { fm.ReadNames(row); } } else { T obj = default(T); try { if (readingRawDataRows) { obj = row as T; } else { obj = fm.ReadObject(row, ae); } } catch (AggregatedException ae2) { throw ae2; } catch (Exception e) { ae.AddException(e); } yield return(obj); } firstRow = false; } } finally { if (readingFile) { stream.Dispose(); } ae.ThrowIfExceptionsStored(); } }