Home > Software engineering >  CsvHelper CsvWriter updated 2.8.4 to 27.1.1
CsvHelper CsvWriter updated 2.8.4 to 27.1.1

Time:10-05

I updated the packet for CsvHelper from updated 2.8.4 to 27.1. and the method I been using has been updated. It is now taking two more arguments. I am not sure what the CsvConfiguration or the CultureInfo is or what I need to do to correct this error.

Error

CsvWriter does not contain a constructor that takes 1 arguments.

New 27.0 methods public CsvWriter(TextWriter writer, CsvConfiguration configuration);

public CsvWriter(TextWriter writer, CultureInfo culture, bool leaveOpen = false);

Code that I been using

using (var fs = new MemoryStream())
   {
   using (var tx = new StreamWriter(fs))
      {
      var csv = new CsvWriter(tx);
      csv.WriteHeader<TemplateCsvModel>();
      csv.WriteRecords(templates);
      csv.Dispose();
      return Encoding.UTF8.GetPreamble().Concat(fs.ToArray()).ToArray();
      }
   }

CodePudding user response:

CultureInfo is needed to account for different formatting & parsings between various cultures as not everyone in the world formats dates, currency etc. the same.

If you don't need to parse your data based on the user's local settings, use CultureInfo.InvariantCulture:

using (var fs = new MemoryStream()) {
    using var tx = new StreamWriter(fs)
    using (var csv = new CsvWriter(tx, CultureInfo.InvariantCulture) {
        csv.WriteHeader<TemplateCsvModel>();
        csv.WriteRecords(templates);
        return Encoding.UTF8.GetPreamble().Concat(fs.ToArray()).ToArray();
      }
}
  • Related