Click here to Skip to main content
15,886,638 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to insert double quote ("") data with comma into one column while  CSV file to datatable using in C# ?


What I have tried:

How to insert double quote ("") data with comma into one column while  CSV file to datatable using in C# ?
Posted
Updated 28-Feb-19 21:01pm

1 solution

CSV Data is pretty primitive, but it can cope with most things. If you want a comma, a newline or a double quote in a field in CSV data, teh whole field needs to be encased in double quotes:
123, Hello there, "A comma, in a field", 666

Which gives you four fields:
123
Hello there
A comma, in a field
666
Inside the double quotes you can include a double quote, but escaping it with a second one:
123, Hello there, "Some ""quoted text""", 666
Which also gives you four fields:
123
Hello there
Some "quoted text"
666
If you want to include that in a C# string literal, then you need to add the C# escapes as well:
C#
string csvData = "123, Hello there, \"Some \"\"quoted text\"\"\", 666";
Or worse:
C#
string csvData = @"123, Hello there, ""Some """"quoted text"""""", 666";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900