private static void WriteToFile(CsvReader csv, StreamWriter file, string format) { try { var sumCD = csv.GetField <int>("columnC") + csv.GetField <int>("columnD"); if (sumCD <= 100) { return; } var data = new Data(csv.GetFieldIndex("columnA"), "ok", csv.GetField <string>("columnA") + csv.GetField <string>("columnB"), sumCD); var line = format.Equals("json") ? JsonConvert.SerializeObject(data) : format.Equals("txt") ? data.ToString() : format.Equals("xml") ? XmlConverter.Serialize(data) : "No format"; file.WriteLine(line + ","); } catch (CsvHelper.TypeConversion.TypeConverterException exception) { var onErrorData = new OnErrorData(csv.GetFieldIndex("columnA"), "error", exception.Message); var line = format.Equals("json") ? JsonConvert.SerializeObject(onErrorData) : format.Equals("txt") ? onErrorData.ToString() : format.Equals("xml") ? XmlConverter.Serialize(onErrorData) : "No format"; file.WriteLine(line); } }