List. CSV. Stream

S

Shapper

Hello,

I have a List<Device> where each Device is:

public class Device {
public String Model { get; set; }
public String Serial { get; set; }
}

I need to create a Stream, to export to a CSV file as follows:

Model1,Serial1;

Model2,Serial2;

Model3,Serial3;

And should I use "," as a value limiter? Is there an alternative?

I am afraid that one model might have a "," in it.

Thank You,

Miguel
 
M

Marcel Müller

Shapper said:
I have a List<Device> where each Device is:

public class Device {
public String Model { get; set; }
public String Serial { get; set; }
}

I need to create a Stream, to export to a CSV file as follows:
And should I use "," as a value limiter?

ask the people who are going to read your CSV stream.

Is there an alternative?

I am afraid that one model might have a "," in it.

CSV is not designed to contain any character in the value fields.

If you use , as column delimiter (as the name CSV suggests) any comma in
the data will break the CSV structure.

If you wrap the columns in double quotes any double quote in the data
will break the CSV structure.

/Some/ programs wrap the columns with quotes and escape double quotes
within data with two double quotes. If /your/ application escapes the
strings this way this won't help anything unless the reading application
uses the same encoding.

If you manage to solve all of the above a line break within the data
will break the CVS structure again. Think of multi line model names
(well not that common).

/Very few/ applications can deal with that, because if you count the
double quotes of the whole file any newline after an even number of
double quotes indicates a record separator, while any newline after an
odd number of double quotes belongs to data. But this prevents simply
software that primarily splits the source stream into lines from working.


Marcel
 
M

Marcel Müller

rossum said:
CSV ignores the quotes, so you can use them even if there are no
commas in the data.

Unfortunately not. /Many/ applications ignore the quotes. But some take
them as data.
If there are no quotes in your data then the problem is solved. If
there are then you can escape the internal quotes, either as "" or as
\" -- another case where you will have to talk to whoever is on the
other end of the link.

Escaping " as \" is uncommon in CSV because it only shifts the problem
from " to \ and then you have to escape \ as \\ as well. But some
applications do this either to escape the line feeds and other control
characters too (like C code does).

If your data is likely not to contain control characters it is often
easier to use tab separated files, since the tabs are less likely to be
embedded in data than the comma.

There is a great deal of information on CSV format at:

(http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm)

Interesting site.


Marcel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top