-1

I am using StreamWriter to write records in csv

 Data = "abcd+ " , " + "Error + " , " + result.Message;
 using (StreamWriter streamWriter = new StreamWriter("c:/temp/data.csv"))
    {
      streamWriter.WriteLine(Data);
    }

Result.Message - Failed to poll for ordered service from Encompass: {\r\n \"code\": \"VPC-999999\",\r\n \"summary\": \"Error occurred in Vendor Platform\",\r\n \"details\": \"Partner error occurred - E206 : Access to report denied\",\r\n \"product\": e

So it has special characters in it.

In csv file I need to have 3 columns with

1st coulmn - abcd

2nd column - Error

3rd column - Failed to poll for ordered service from Encompass: {\r\n \"code\": \"VPC-999999\",\r\n \"summary\": \"Error occurred in Vendor Platform\",\r\n \"details\": \"Partner error occurred - E206 : Access to report denied\",\r\n \"product\": e

Since this long text contains special chars, in csv it gets written to next line and so. How to treat this whole message as 1 text and can put it in 1 cell?

  • 3
    Not very clear what is so "special" about characters in your example. Also it makes no sense to write CSV writer/parser by hand - clarify why you must do it hard way. Please [edit] post with that info. – Alexei Levenkov Apr 26 '22 at 23:23
  • Try surrounding your `result.Message` with double quotes. I have no experience working in C# but I have worked with CSV in other languages. Generally, we wrap the whole column with quotes( " " ) and that should do the trick to treat all the contents as one column ignoring any special characters. – Imtiaz Chowdhury Apr 26 '22 at 23:26
  • 1
    @ImtiazChowdhury you're right that that is probably the problem. But it's not as simple as just wrapping the whole thing in quotes. The problem is that the inner set of quotes is not being escaped, so wrapping the outer portion with quotes is just going to create a different problem. The straightforward solution, as Alexei mentions, is to not handroll your own CSV serializer and use a library that takes care of these nuances for you, of which there are plenty of options in C#. – Kirk Woll Apr 26 '22 at 23:34
  • @KirkWoll can you give me any example? – Kavita Korgaonkar Apr 26 '22 at 23:49
  • 1
    I believe [CsvHelper](https://joshclose.github.io/CsvHelper/) is the most popular choice. – Kirk Woll Apr 27 '22 at 00:01

1 Answers1

0

A part of the problem is that the "CSV format" is... poorly defined... to say the least. The Microsoft Excel implementation is probably as close to a "reference implementation" as you'll get and it's got some weird corners in it. There is also RFC 4180 that defines a ABNF grammar for CSV data.

But rather than rolling your own, I would use CSV tooling that has had the kinks worked out of it.

  • Sebastien Lorion's Fast CSV Reader is a good tool, but a reader only.

  • FastCSV, looks interesting, but I can't say I've used it.

  • Josh Close's CsvHelper also looks good, but again, I haven't used it.

I'm sure there are others as well.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135