Note
In the following code examples - for easier readability - exception handling has been omitted.
public static void FillWith(
this CsvRecord record,
ReadOnlySpan<ReadOnlyMemory<char>> data,
bool resetExcess = true
)
<ExtensionAttribute>
Public Shared Sub FillWith (
record As CsvRecord,
data As ReadOnlySpan(Of ReadOnlyMemory(Of Char)),
Optional resetExcess As Boolean = true
)
public:
[ExtensionAttribute]
static void FillWith(
CsvRecord^ record,
ReadOnlySpan<ReadOnlyMemory<wchar_t>> data,
bool resetExcess = true
)
[<ExtensionAttribute>]
static member FillWith :
record : CsvRecord *
data : ReadOnlySpan<ReadOnlyMemory<char>> *
?resetExcess : bool
(* Defaults:
let _resetExcess = defaultArg resetExcess true
*)
-> unit
If data has fewer items than record fields and this parameter is true, the surplus fields in record will be reset to the default value .
For performance reasons this parameter can be set to false when writing CSV because WriteRecord resets all fields in record.
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,,
*/
ArgumentNullException | record is null. |
ArgumentOutOfRangeException | data contains more items than record. |