LinqToCSV How to Eliminate Quotes Around Blank Lines
LinqToCSV seems to add quotes around empty lines Is there a setting to turn this feature off? Couldn't find anything in the documentation. Also tried to send null values instead of empty strings. The same result
this is an example of CSV output generated using LinqToCSV. Columns 5,6,7 are empty lines .. they end up as quoted lines in the final CSV output
3223,2876,5591,9171068, "," "," ", 10000442, A1," ", 012000016431,2.50,8,0,8,20150108224612 3223,2876,5591,9171068,", "", "", 10000442, A2, "", 012000016431,2.50,8,0,8,20150108224612 3223,2876,5591,9171068, "," "," ", 10000442, A3," ", 012000043000,1.75,8,0,8 , 20150108224612 3223,2876,5591,9171068, "," "," ", 10000442, A4," ", 012000043000,1.75,8,0,8,20150108224612 3223,2876,5591,9171068,", "", " ", 10000442, A5," ", 012000043000,1.75,8,0,8,20150108224612 3223,2876,5591,9171068,", "", "", 10000442, A6, "", 012000110467,1.75,8,0 , 8.20150108224612
this is sample code for link
CsvFileDescription fd = new CsvFileDescription();
fd.FirstLineHasColumnNames = false;
fd.QuoteAllFields = false;
fd.EnforceCsvColumnAttribute = true;
fd.SeparatorChar = Globals.Separator[0];
CsvContext cc = new CsvContext();
cc.Write(data, txtFileName, fd);
All data properties are simple strings
for example one of the columns is defined as
[CsvColumn(FieldIndex = 16, CanBeNull = true)]
public string TimeStamp { get; set; }
source to share
This behavior is not reproducible with LinqToCsv v1.5 . When I set the value of the field TimeStamp
to null
, the value is not specified. However, the value is quoted String.Empty
.
Consider the following:
void Main()
{
var fd = new CsvFileDescription();
fd.FirstLineHasColumnNames = false;
fd.QuoteAllFields = false;
fd.EnforceCsvColumnAttribute = true;
fd.SeparatorChar = ',';
var txtFileName = @"C:\temp\linqtocsv.csv";
var data = new[] { new Data { TimeStamp = null, Foo = null, Bar = "" } };
var cc = new CsvContext();
cc.Write(data, txtFileName, fd);
}
class Data
{
[CsvColumn(FieldIndex = 0, CanBeNull = true)]
public string TimeStamp { get; set; }
[CsvColumn(FieldIndex = 1, CanBeNull = true)]
public string Foo { get; set; }
[CsvColumn(FieldIndex = 2, CanBeNull = true)]
public string Bar { get; set; }
}
Output file content:
,,""
You can try it yourself: this query is LinqPad .
source to share