I am trying to write to a CSV file with the help of CSVHelper, one of the values i am writing that has values like this: {1,$1.00,2,$3.00}
Due to the commas in the list it is double quoting this in the output, but I am wanting to disable this functionality.
I've read the documentation which suggests using QuoteNoFields = true, however I get the error
IReaderConfiguration does not contain a definition for QuoteNoFields when I try to run the project.
Has this method been deprecated, or am i doing something incorrectly?
try
{
using (TextReader reader = File.OpenText(filepath))
using (StreamWriter sw = new StreamWriter(@"C:\temp\mi_imp.txt"))
using (CsvWriter writer = new CsvWriter(sw))
{
var csv = new CsvReader(reader);
csv.Configuration.Delimiter = ",";
csv.Configuration.HasHeaderRecord = true;
csv.Configuration.HeaderValidated = null;
csv.Configuration.MissingFieldFound = null;
csv.Configuration.IgnoreQuotes = true;
csv.Configuration.QuoteNoFields = true;
var usersFromCsv = csv.GetRecords<ProductItem>();
statuslabel.Content = "Status: Reading CSV";
// ********************
string menuItemNametemp;
string buttontxt1temp;
string buttontxt2temp;
string kitchenprintertexttemp;
string customerreceipttexttemp;
string kitchenvideotexttemp;
// ********************
foreach (var item in usersFromCsv)
{
// **** reset to null ****
menuItemNametemp = "";
buttontxt1temp = "";
buttontxt2temp = "";
kitchenprintertexttemp = "";
customerreceipttexttemp = "";
kitchenvideotexttemp = "";
// ************************
// ****** set default values *******
item.Action = "A";
item.PriceLevelID = "{1,$0.00}";
item.SecurityLevel = "0";
item.UseWeightFlag = "0";
item.WeightTare = "0";
item.BarGunCode = "";
// ********* build strings ************
buttontxt1temp = @"""" + item.ButtonTxt1 + @"""" + ",";
buttontxt2temp = @"""" + item.ButtonTxt2 + @"""" + ",";
menuItemNametemp = @"""" + item.MenuItemName + @"""" + ",";
kitchenprintertexttemp = @"""" + item.KitchenPrinterLabel + @""",";
customerreceipttexttemp = @"""" + item.ReceiptText + @""",";
kitchenvideotexttemp = @"""" + item.KitchenVideoText + @""",";
// *************************************
// *********** transfer to object *******
item.ButtonTxt1 = buttontxt1temp;
item.ButtonTxt2 = buttontxt2temp;
item.MenuItemName = menuItemNametemp;
item.KitchenPrinterLabel = kitchenprintertexttemp;
item.ReceiptText = customerreceipttexttemp;
item.KitchenVideoText = kitchenvideotexttemp;
// ****************************************
writer.WriteRecord(item);
}
statuslabel.Content = "Status: Complete";
}
}
catch (Exception ex)
{
LogWriter log = new LogWriter(ex.ToString());
statuslabel.Content = "Status: Error";
textcontent.Text = "";
MessageBox.Show("" + ex, "Error");
}
I expect my results to look like this:
"A",9600015,"Date Tart","Date","Tart","Date Tart",{1,$0.00},76,7,1,0,1,0,0,{},,$0.00,0,0,1,1,1,0,1,1,"Date Tart",1,0,{215,0},{},0,0,"Date Tart",0,0,0,{},0
but instead I get something like this
"A",9600015,"Date Tart","Date","Tart","Date Tart","{1,$0.00}",76,7,1,0,1,0,0,{},,$0.00,0,0,1,1,1,0,1,1,"Date Tart",1,0,"{215,0}",{},0,0,"Date Tart",0,0,0,{},0
After some searching, I found the solution.
While I thought the method had been deprecated because it was not appearing under the csv.configuration - I realised I should be setting this configuration setting under the writer instantiation.
using (TextReader reader = File.OpenText(filepath))
using (StreamWriter sw = new StreamWriter(@"C:\temp\mi_imp.txt"))
using (CsvWriter writer = new CsvWriter(sw))
var csv = new CsvReader(reader);
// what i was doing previously - incorrect
csv.configuration.quotenofields = true;
// correct solution
writer.configuration.quotenofields = true;