Class CsvEscaping
Turns raw values into RFC 4180 CSV fields, so a value carrying a comma, a quote, a line break or significant whitespace survives the trip into a spreadsheet or a storefront's bulk importer intact.
Inherited Members
Namespace: Serenity.Shared.Utils
Assembly: Serenity.Shared.dll
Syntax
public static class CsvEscaping
Remarks
Shared rather than private to whichever exporter needed it first, because the rule it encodes is the one every CSV writer has to get right and the one every hand-rolled writer gets wrong the same way: a field is quoted when it contains a comma, a quote, a carriage return or a line feed, and an inner quote is doubled.
Leading and trailing whitespace forces quoting too, which RFC 4180 does not require and which every
consumer worth exporting to needs: readers routinely trim an unquoted field, so " Jump" written
bare comes back as "Jump" — a silent edit to the author's data. Quoting is the only way to say
the space was meant.
Line endings are the caller's choice: AppendRow(StringBuilder, IEnumerable<string>) writes the fields and the separators and nothing else, so an exporter that promises LF-only output can keep that promise.
Methods
AppendRow(StringBuilder, IEnumerable<string>)
Appends a whole row to a builder, with no line ending.
Declaration
public static void AppendRow(StringBuilder builder, IEnumerable<string> values)
Parameters
| Type | Name | Description |
|---|---|---|
| StringBuilder | builder | The builder to append to. Null appends nothing. |
| IEnumerable<string> | values | The raw values, in column order. Null appends nothing. |
Field(string)
Renders one value as a CSV field, quoted and escaped only when it has to be.
Declaration
public static string Field(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The raw value. Null is treated as empty. |
Returns
| Type | Description |
|---|---|
| string | The field, ready to be written between separators. |