CsvWriterWriteRecord Method

Writes the contents of Record to the CSV file and then sets all fields of Record to Empty. (The first time it is called, the header row may also be written.)

Definition

Namespace: FolkerKinzel.CsvTools
Assembly: FolkerKinzel.CsvTools (in FolkerKinzel.CsvTools.dll) Version: 2.0.1+2345335399184346d9b2cc992ed5c814406052c1
C#
public void WriteRecord()

Example

  Note

In the following code examples - for easier readability - exception handling has been omitted.
C#
using System.Text;
using FolkerKinzel.CsvTools;

namespace Examples;

internal static class CsvAnalyzerExample
{
    public static void ParseForeignCsvFile(string filePath)
    {
        const string nonStandardCsv = """


            First # "Second # Column"   
            1,"2",3 # "Get's
            too much" # LOST?

            too few


            """;

        File.WriteAllText(filePath, nonStandardCsv, Encoding.Unicode);

        using CsvReader csv = Csv.OpenReadAnalyzed(filePath);
        CsvRecord[] data = [.. csv];

        using (CsvWriter writer = Csv.OpenWrite(filePath, data[0].ColumnNames))
        {
            foreach (CsvRecord record in data)
            {
                writer.Record.FillWith(record.Values);
                writer.WriteRecord();
            }
        }

        Console.WriteLine(File.ReadAllText(filePath));
    }
}

/*
 Console output:

Column1,Column2,Column3
First ,Second # Column,
"1,""2"",3 ","Get's
too much", LOST?
too few,,

*/

Exceptions

IOExceptionI/O error.
ObjectDisposedExceptionThe file was already closed.

See Also