Ejemplo n.º 1
0
 /// <summary>
 /// Read a file as CSV, using specific behaviour, layout and conversion options 
 /// </summary>
 /// <param name="path">The full or relative path name</param>
 /// <param name="encoding">The encoding</param>
 /// <param name="csvLayout">Csv layout info (quote, delimiter, ...)</param>
 /// <param name="csvBehaviour">Csv behaviour info (e.g. what to do when fields are missing)</param>
 /// <param name="converter">Converter class for converting strings to primitive types (used by the data reader</param>
 /// <param name="bufferSize">The number of characters to buffer while parsing the CSV.</param>
 /// <returns>a datareader instance to read the contents of the CSV file</returns>
 public static IDataReader ReadFileAsCsv(this string path, Encoding encoding, CsvLayout csvLayout, CsvBehaviour csvBehaviour, Converter converter, int bufferSize = 4096)
 {
     StreamReader reader = null;
     FileStream stream = null;
     try
     {
         stream = File.OpenRead(path);
         reader = new StreamReader(stream, encoding);
         return ReadStreamAsCsv(reader, csvLayout, csvBehaviour, converter, bufferSize);
     }
     catch
     {
         if (reader != null) reader.Dispose();
         else if (stream != null) stream.Dispose();
         throw;
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Read a string as CSV, using specific behaviour, layout and conversion options 
 /// </summary>
 /// <param name="input">The CSV input</param>
 /// <param name="csvLayout">Csv layout info (quote, delimiter, ...)</param>
 /// <param name="csvBehaviour">Csv behaviour info (e.g. what to do when fields are missing)</param>
 /// <param name="converter">Converter class for converting strings to primitive types (used by the data reader</param>
 /// <param name="bufferSize">The number of characters to buffer while parsing the CSV.</param>
 /// <returns>a datareader instance to read the contents of the CSV file</returns>
 public static IDataReader ReadStringAsCsv(this string input, CsvLayout csvLayout, CsvBehaviour csvBehaviour, Converter converter, int bufferSize = 4096)
 {
     StringReader reader = null;
     try
     {
         reader = new StringReader(input);
         return ReadStreamAsCsv(reader, csvLayout, csvBehaviour, converter, bufferSize);
     }
     catch
     {
         if (reader != null) reader.Dispose();
         throw;
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Read a stream as CSV, using specific behaviour, layout and conversion options 
 /// </summary>
 /// <param name="reader">A <see cref="TextReader"/> instance</param>
 /// <param name="csvLayout">Csv layout info (quote, delimiter, ...)</param>
 /// <param name="csvBehaviour">Csv behaviour info (e.g. what to do when fields are missing)</param>
 /// <param name="converter">Converter class for converting strings to primitive types (used by the data reader</param>
 /// <param name="bufferSize">The number of characters to buffer while parsing the CSV.</param>
 /// <returns>a datareader instance to read the contents of the CSV file</returns>
 public static IDataReader ReadStreamAsCsv(this TextReader reader, CsvLayout csvLayout, CsvBehaviour csvBehaviour, Converter converter, int bufferSize = 4096)
 {
     var parser = new CsvParser(reader, csvLayout, csvBehaviour);
     return new CsvDataReader(parser, converter);
 }