Exemplo n.º 1
0
 /// <summary>
 /// Method is meant to write a file with a set of models.
 /// </summary>
 /// <param name="path">The path to the file we are saving to.</param>
 /// <param name="models">The models we are saving.</param>
 /// <param name="options">The options that should be sent off to the stream.</param>
 /// <typeparam name="TModel">The model the parser needs to translate from.</typeparam>
 public static void SaveFile <TModel>(string path, IEnumerable <TModel> models, CsvStreamOptions options)
     where TModel : class, new()
 {
     using (var writer = new CsvStreamWriter <TModel>(path, options))
     {
         if (options.WriteHeaders)
         {
             writer.WriteHeader();
         }
         foreach (var model in models)
         {
             writer.WriteLine(model);
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Method is meant to parse out the models to a string for use somewhere else.
        /// </summary>
        /// <param name="models">The models that need to be parsed into a string.</param>
        /// <param name="options">The options that should be sent off to the stream.</param>
        /// <typeparam name="TModel">The model the parser needs to translate from.</typeparam>
        /// <returns>Will return a parsed string of the objects being passed in.</returns>
        public static string Stringify <TModel>(IEnumerable <TModel> models, CsvStreamOptions options)
            where TModel : class, new()
        {
            var result = string.Empty;
            var stream = GenerateStream(string.Empty);

            using (var writer = new CsvStreamWriter <TModel>(stream, options))
            {
                if (options.WriteHeaders)
                {
                    writer.WriteHeader();
                }
                foreach (var model in models)
                {
                    writer.WriteLine(model);
                }
                writer.Flush();
                result = ReadStream(stream);
            }
            return(result);
        }