1

I am trying to follow everybody's advice and use a library for my CSV writing/parsing needs. But the most recommended, FileHelpers, needs a column-mapping class. However, I just need to read write single lines of comma separated values, then parse them back later. I can't find a way to use FileHelpers or other libraries in this simple way.

For example, I make a String testInput = "\"yoyo\",\"yaya\",\"te\"\"st\""; as input, and want to parse it into a String[] with values yoyo,yaya,te"st.

Also I need to do the opposite, writing the values back to the string like this "yoyo","yaya","te"st".

Does anyone know of a library that can do this or how I can use for example FileHelpers for this?

I don't really want to do xml because of the amount of "junk" characters involved in xml fields (bandwidth/storage limitations).

David S.
  • 5,965
  • 2
  • 40
  • 77

2 Answers2

2

The CSV reader on code project should handle that; just turn off the file headers and use new StringReader(line) on a line-by-line basis.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

Microsoft.VisualBasic.FileIO.TextFieldParser should be able to do the job, ReadFields method which is defined as:

Reads all fields on the current line, returns them as an array of strings, and advances the cursor to the next line containing data.

I found nice example here.

As for writing CSV, I'd use different method that I found on stackoverflow. It seems to be the simplest method that doesn't require importing 3rd party extension.

Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96
  • Thank you, but I also need a writer to correctly convert fields to csv, so that is only half of the solution. – David S. Apr 02 '13 at 11:19
  • 1
    @David writing CSV is easier than reading it - especially if you want all the values quoted (as in this case). Replace `\"` with `\"\"` and append+prepend `\"` - job done. – Marc Gravell Apr 02 '13 at 11:20
  • 1
    @David there's no CSV writer class simple enough for you... But I've added a link that I found. – Vyktor Apr 02 '13 at 11:25