David S.
David S.

Reputation: 6105

Writing/Parsing library for CSV with unknown number of columns

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).

Upvotes: 1

Views: 2870

Answers (2)

Vyktor
Vyktor

Reputation: 20997

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.

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062855

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.

Upvotes: 2

Related Questions