2

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

Asher
  • 348
  • 1
  • 3
  • 19
  • Then what output do you want? – SLaks Dec 08 '17 at 01:34
  • @slak The desired format should be {1,$1.00,2,$2.00} e.g., as above without quotes surrounding the value. – Asher Dec 08 '17 at 01:36
  • 1
    That's not a valid CSV. – SLaks Dec 08 '17 at 01:37
  • @SLaks The { } acts as a delimiter for certain fields with multiple values. – Asher Dec 08 '17 at 01:39
  • @Ash could you post the code you're using to write the fields. Looking at https://joshclose.github.io/CsvHelper/writing#writing-fields looks like you should be able to specify if you want to quote the field at the time of writing. Also could you post current results (as compared to what you want) to make it clear. – ameer Dec 08 '17 at 01:44
  • @ameer I have updated my question with more information. Yes, from reading the documentation there is an option mentioned called QuoteNoFields, but in my package it appears this has been deprecated. – Asher Dec 08 '17 at 01:52

3 Answers3

2
    public void CreateCsv()
    {
        // populate data with records
        IEnumerable<SampleDataClass> data;

        var _csvConfiguration = new CsvHelper.Configuration.Configuration {
            ShouldQuote = (field, ctx) => false
        };
        using (var file = File.CreateText(_outputFileName))
        using (var csv = new CsvWriter(file, _csvConfiguration))
        {
            csv.WriteHeader<SampleDataClass>();

            csv.NextRecord();
            while (data.MoveNext())
            {
                csv.WriteRecord(data.Current);
                csv.NextRecord();
            }
        }
    }


    public class SampleDataClass
    {
        public string product_id { get; set; }

        public string order_id { get; set; }

        public string user_id { get; set; }

    }
Tom
  • 21
  • 3
1

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;
Asher
  • 348
  • 1
  • 3
  • 19
1

The way to turn off quotes has changed again. With CsvHelper 27.1.1, this works:

var csvConfig = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.CurrentCulture)
{
    ShouldQuote = args => false
};

using var fs = new StreamWriter("data.csv");
using var writer = new CsvWriter(fs, csvConfig);

Unfortunately, the library is poorly documented. I have figured it out from this issue: https://github.com/JoshClose/CsvHelper/issues/1726

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77