API Reference
CsvDocument
Represents a CSV document with a fluent, document-centric API. Thread-safe for independent read enumeration; not thread-safe for concurrent mutations on the same instance.
Inheritance
- Object
- CsvDocument
Usage
This type appears in these public API surfaces even when no hand-authored example is attached directly to the page.
Returned or exposed by
- Method CsvDocument.AddColumn
- Method CsvDocument.AddRow
- Method CsvDocument.EnsureInferredSchema
- Method CsvDocument.EnsureSchema
- Method CsvDocument.Filter
- Method CsvDocument.FromObjects
- Method CsvDocument.Load
- Method CsvDocument.LoadAsync
- Method CsvDocument.Materialize
- Method CsvDocument.Parse
- Method CsvDocument.RemoveColumn
- Method CsvDocument.SortBy
- Method CsvDocument.Transform
- Method CsvDocument.Validate
- Method CsvDocument.ValidateOrThrow
- Method CsvDocument.WithCulture
- Method CsvDocument.WithDateTimeFormats
- Method CsvDocument.WithDelimiter
- Method CsvDocument.WithEncoding
- Method CsvDocument.WithHeader
Accepted by parameters
- Extension method CsvDocument.Map
- Method CsvDocument.Transform
- Method CsvMappingExtensions.Map
- Method CsvMappingExtensions.Map``1
Constructors
public CsvDocument() #Initializes a new in-memory CSV document with default settings.
Methods
public CsvDocument AddColumn(String name, Func<CsvRow, Object> valueFactory) #CsvDocumentAdds a computed column to the document.
Parameters
- name System.String
- valueFactory System.Func{OfficeIMO.CSV.CsvRow,System.Object}
public IEnumerable<CsvRow> AsEnumerable() #IEnumerable<CsvRow>Returns rows as an enumerable sequence. In streaming mode this is lazy.
public static CsvDataReader CreateDataReader(String path, CsvLoadOptions loadOptions = null, CsvDataReaderOptions readerOptions = null) #CsvDataReaderCreates a forward-only data reader over a CSV file.
Parameters
- path System.String
- Source CSV path.
- loadOptions OfficeIMO.CSV.CsvLoadOptions = null
- CSV load options.
- readerOptions OfficeIMO.CSV.CsvDataReaderOptions = null
- Reader projection options. When omitted, all columns are emitted as strings.
Returns
A data reader suitable for DataTable loading and provider bulk-copy APIs.
public CsvDataReader CreateDataReader(CsvDataReaderOptions options = null) #CsvDataReaderCreates a forward-only data reader over the document rows.
Parameters
- options OfficeIMO.CSV.CsvDataReaderOptions = null
- Reader projection options. When omitted, all columns are emitted as strings.
Returns
A data reader suitable for DataTable loading and provider bulk-copy APIs.
public CsvDocument EnsureInferredSchema(Int32 sampleSize = 1000) #CsvDocumentInfers and attaches a schema to the document so subsequent validation uses the sampled structure.
Parameters
- sampleSize System.Int32 = 1000
- Maximum number of rows to inspect. Defaults to 1000.
Returns
The current document.
public CsvDocument EnsureSchema(Action<CsvSchemaBuilder> buildAction) #CsvDocumentAttaches a schema to the document.
Parameters
- buildAction System.Action{OfficeIMO.CSV.CsvSchemaBuilder}
public CsvDocument Filter(Func<CsvRow, Boolean> predicate) #CsvDocumentFilters rows using the provided predicate.
Parameters
- predicate System.Func{OfficeIMO.CSV.CsvRow,System.Boolean}
public static CsvDocument FromObjects(IEnumerable<Object> items, Char delimiter = ',', CultureInfo culture = null, Encoding encoding = null) #CsvDocumentCreates a CSV document from a sequence of objects by projecting their properties or dictionary keys into columns.
Parameters
- items System.Collections.Generic.IEnumerable{System.Object}
- Sequence of objects to convert into CSV rows.
- delimiter System.Char = ','
- Delimiter to use for the CSV document.
- culture System.Globalization.CultureInfo = null
- Optional culture for value formatting.
- encoding System.Text.Encoding = null
- Optional encoding for file operations.
Returns
A populated CsvDocument.
public CsvSchema InferSchema(Int32 sampleSize = 1000) #CsvSchemaInfers a schema from the document header and sampled row values.
Parameters
- sampleSize System.Int32 = 1000
- Maximum number of rows to inspect. Defaults to 1000.
Returns
A schema containing one inferred column for each header field.
public static CsvDocument Load(Stream stream, CsvLoadOptions options = null) #CsvDocumentLoads a CSV document from disk.
Parameters
- path System.String
- options OfficeIMO.CSV.CsvLoadOptions = null
Load(System.IO.Stream stream, OfficeIMO.CSV.CsvLoadOptions options) #Loads a CSV document from a stream.
Parameters
- stream System.IO.Stream
- Source stream.
- options OfficeIMO.CSV.CsvLoadOptions
- Load options.
public static async Task<CsvDocument> LoadAsync(Stream stream, CsvLoadOptions options = null, CancellationToken cancellationToken = null) #Task<CsvDocument>Asynchronously loads a CSV document from disk.
Parameters
- path System.String
- options OfficeIMO.CSV.CsvLoadOptions = null
- cancellationToken System.Threading.CancellationToken = null
LoadAsync(System.IO.Stream stream, OfficeIMO.CSV.CsvLoadOptions options, System.Threading.CancellationToken cancellationToken) #Asynchronously loads a CSV document from a caller-owned stream.
Parameters
- stream System.IO.Stream
- options OfficeIMO.CSV.CsvLoadOptions
- cancellationToken System.Threading.CancellationToken
public CsvDocument Materialize() #CsvDocumentForces a streaming document to materialize into memory, enabling transformations.
public static CsvDocument Parse(String text, CsvLoadOptions options = null) #CsvDocumentParses a CSV document from text.
Parameters
- text System.String
- options OfficeIMO.CSV.CsvLoadOptions = null
public static Void ReadFieldSpans<TVisitor>(ReadOnlySpan<Char> text, ref TVisitor fieldVisitor, CsvLoadOptions options = null) #VoidReads CSV fields from a file in a single pass without materializing unquoted fields as strings.
Type Parameters
- TVisitor
Parameters
- path System.String
- Source CSV path.
- fieldAction OfficeIMO.CSV.CsvFieldSpanAction
- Action receiving each field as a transient span.
- options OfficeIMO.CSV.CsvLoadOptions = null
- Optional load settings. Header handling is not applied; records are emitted as parsed.
ReadFieldSpans(System.IO.TextReader reader, OfficeIMO.CSV.CsvFieldSpanAction fieldAction, OfficeIMO.CSV.CsvLoadOptions options) #Reads CSV fields from a reader in a single pass without materializing unquoted fields as strings.
Parameters
- reader System.IO.TextReader
- Source text reader.
- fieldAction OfficeIMO.CSV.CsvFieldSpanAction
- Action receiving each field as a transient span.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings. Header handling is not applied; records are emitted as parsed.
public static Void ReadFieldSpansFromText<TVisitor>(String text, ref TVisitor fieldVisitor, CsvLoadOptions options = null) #VoidReads CSV fields from text in a single pass without materializing unquoted fields as strings.
Type Parameters
- TVisitor
Parameters
- text System.String
- Source CSV text.
- fieldAction OfficeIMO.CSV.CsvFieldSpanAction
- Action receiving each field as a transient span.
- options OfficeIMO.CSV.CsvLoadOptions = null
- Optional load settings. Header handling is not applied; records are emitted as parsed.
ReadFieldSpansFromText``1(System.String text, ``0@ fieldVisitor, OfficeIMO.CSV.CsvLoadOptions options) #Reads CSV fields from text in a single pass without materializing unquoted fields as strings.
Type Parameters
- TVisitor
- Struct visitor type receiving each field.
Parameters
- text System.String
- Source CSV text.
- fieldVisitor ``0@
- Visitor receiving each field as a transient span.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings. Header handling is not applied; records are emitted as parsed.
ReadFieldSpans``1(System.String path, ``0@ fieldVisitor, OfficeIMO.CSV.CsvLoadOptions options) #Reads CSV fields from a file in a single pass without materializing unquoted fields as strings.
Type Parameters
- TVisitor
- Struct visitor type receiving each field.
Parameters
- path System.String
- Source CSV path.
- fieldVisitor ``0@
- Visitor receiving each field as a transient span.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings. Header handling is not applied; records are emitted as parsed.
ReadFieldSpans``1(System.IO.TextReader reader, ``0@ fieldVisitor, OfficeIMO.CSV.CsvLoadOptions options) #Reads CSV fields from a reader in a single pass without materializing unquoted fields as strings.
Type Parameters
- TVisitor
- Struct visitor type receiving each field.
Parameters
- reader System.IO.TextReader
- Source text reader.
- fieldVisitor ``0@
- Visitor receiving each field as a transient span.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings. Header handling is not applied; records are emitted as parsed.
ReadFieldSpans``1(System.ReadOnlySpan{System.Char} text, ``0@ fieldVisitor, OfficeIMO.CSV.CsvLoadOptions options) #Reads CSV fields from text in a single pass without materializing unquoted fields as strings.
Type Parameters
- TVisitor
- Struct visitor type receiving each field.
Parameters
- text System.ReadOnlySpan{System.Char}
- Source CSV text.
- fieldVisitor ``0@
- Visitor receiving each field as a transient span.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings. Header handling is not applied; records are emitted as parsed.
public static IEnumerable<String[]> ReadRecords(TextReader reader, CsvLoadOptions options = null) #IEnumerable<String[]>Reads raw CSV records from a file as an enumerable sequence.
Parameters
- path System.String
- Source CSV path.
- options OfficeIMO.CSV.CsvLoadOptions = null
- Optional load settings. Header handling is not applied; records are emitted as parsed.
ReadRecords(System.IO.TextReader reader, OfficeIMO.CSV.CsvLoadOptions options) #Reads raw CSV records from a reader as an enumerable sequence.
Parameters
- reader System.IO.TextReader
- Source text reader.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings. Header handling is not applied; records are emitted as parsed.
public static Void ReadRecordsReusable(TextReader reader, Action<IReadOnlyList<String>> recordAction, CsvLoadOptions options = null) #VoidReads raw CSV records from a file in a single pass while reusing the record value buffer for unquoted records.
Parameters
- path System.String
- Source CSV path.
- recordAction System.Action{System.Collections.Generic.IReadOnlyList{System.String}}
- Action receiving the current record values. Record values must not be captured after the callback returns.
- options OfficeIMO.CSV.CsvLoadOptions = null
- Optional load settings. Header handling is not applied; records are emitted as parsed.
ReadRecordsReusable(System.IO.TextReader reader, System.Action{System.Collections.Generic.IReadOnlyList{System.String}} recordAction, OfficeIMO.CSV.CsvLoadOptions options) #Reads raw CSV records from a reader in a single pass while reusing the record value buffer for unquoted records.
Parameters
- reader System.IO.TextReader
- Source text reader.
- recordAction System.Action{System.Collections.Generic.IReadOnlyList{System.String}}
- Action receiving the current record values. Record values must not be captured after the callback returns.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings. Header handling is not applied; records are emitted as parsed.
public static Void ReadRowFieldSpans<TVisitor>(ReadOnlySpan<Char> text, ref TVisitor rowVisitor, CsvLoadOptions options = null) #VoidType Parameters
- TVisitor
Parameters
- path String
- rowVisitor TVisitor
- options CsvLoadOptions = null
public static Void ReadRowFieldSpansFromText<TVisitor>(String text, ref TVisitor rowVisitor, CsvLoadOptions options = null) #VoidType Parameters
- TVisitor
Parameters
- text String
- rowVisitor TVisitor
- options CsvLoadOptions = null
ReadRowFieldSpansFromText``1(System.String text, ``0@ rowVisitor, OfficeIMO.CSV.CsvLoadOptions options) #Reads CSV data rows from text in a single pass, applying header discovery while avoiding string materialization for unquoted data fields.
Type Parameters
- TVisitor
- Struct visitor type receiving each data row and field.
Parameters
- text System.String
- Source CSV text.
- rowVisitor ``0@
- Visitor receiving normalized headers and transient data field spans.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings.
ReadRowFieldSpans``1(System.String path, ``0@ rowVisitor, OfficeIMO.CSV.CsvLoadOptions options) #Reads CSV data rows from a file in a single pass, applying header discovery while avoiding string materialization for unquoted data fields.
Type Parameters
- TVisitor
- Struct visitor type receiving each data row and field.
Parameters
- path System.String
- Source CSV path.
- rowVisitor ``0@
- Visitor receiving normalized headers and transient data field spans.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings.
ReadRowFieldSpans``1(System.IO.TextReader reader, ``0@ rowVisitor, OfficeIMO.CSV.CsvLoadOptions options) #Reads CSV data rows from a reader in a single pass, applying header discovery while avoiding string materialization for unquoted data fields.
Type Parameters
- TVisitor
- Struct visitor type receiving each data row and field.
Parameters
- reader System.IO.TextReader
- Source text reader.
- rowVisitor ``0@
- Visitor receiving normalized headers and transient data field spans.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings.
ReadRowFieldSpans``1(System.ReadOnlySpan{System.Char} text, ``0@ rowVisitor, OfficeIMO.CSV.CsvLoadOptions options) #Reads CSV data rows from text in a single pass, applying header discovery while avoiding string materialization for unquoted data fields.
Type Parameters
- TVisitor
- Struct visitor type receiving each data row and field.
Parameters
- text System.ReadOnlySpan{System.Char}
- Source CSV text.
- rowVisitor ``0@
- Visitor receiving normalized headers and transient data field spans.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings.
public static Void ReadRows(TextReader reader, Action<IReadOnlyList<String>, IReadOnlyList<String>> rowAction, CsvLoadOptions options = null) #VoidReads a CSV file in a single pass and invokes an action for each data row.
Parameters
- path System.String
- Source CSV path.
- rowAction System.Action{System.Collections.Generic.IReadOnlyList{System.String},System.Collections.Generic.IReadOnlyList{System.String}}
- Action receiving the header and current row values.
- options OfficeIMO.CSV.CsvLoadOptions = null
- Optional load settings.
ReadRows(System.IO.TextReader reader, System.Action{System.Collections.Generic.IReadOnlyList{System.String},System.Collections.Generic.IReadOnlyList{System.String}} rowAction, OfficeIMO.CSV.CsvLoadOptions options) #Reads CSV data in a single pass and invokes an action for each data row.
Parameters
- reader System.IO.TextReader
- Source text reader.
- rowAction System.Action{System.Collections.Generic.IReadOnlyList{System.String},System.Collections.Generic.IReadOnlyList{System.String}}
- Action receiving the header and current row values.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings.
public static Void ReadRowsReusable(TextReader reader, Action<IReadOnlyList<String>, IReadOnlyList<String>> rowAction, CsvLoadOptions options = null) #VoidReads a CSV file in a single pass while reusing the row value buffer for unquoted rows.
Parameters
- path System.String
- Source CSV path.
- rowAction System.Action{System.Collections.Generic.IReadOnlyList{System.String},System.Collections.Generic.IReadOnlyList{System.String}}
- Action receiving the header and current row values. Row values must not be captured after the callback returns.
- options OfficeIMO.CSV.CsvLoadOptions = null
- Optional load settings.
ReadRowsReusable(System.IO.TextReader reader, System.Action{System.Collections.Generic.IReadOnlyList{System.String},System.Collections.Generic.IReadOnlyList{System.String}} rowAction, OfficeIMO.CSV.CsvLoadOptions options) #Reads CSV data in a single pass while reusing the row value buffer for unquoted rows.
Parameters
- reader System.IO.TextReader
- Source text reader.
- rowAction System.Action{System.Collections.Generic.IReadOnlyList{System.String},System.Collections.Generic.IReadOnlyList{System.String}}
- Action receiving the header and current row values. Row values must not be captured after the callback returns.
- options OfficeIMO.CSV.CsvLoadOptions
- Optional load settings.
public CsvDocument RemoveColumn(String name) #CsvDocumentRemoves a column by name.
Parameters
- name System.String
public Void Save(Stream destination, CsvSaveOptions options = null) #VoidSaves the document to the specified path.
Parameters
- path System.String
- options OfficeIMO.CSV.CsvSaveOptions = null
Save(System.IO.Stream destination, OfficeIMO.CSV.CsvSaveOptions options) #Saves the document to a caller-owned writable stream.
Parameters
- destination System.IO.Stream
- options OfficeIMO.CSV.CsvSaveOptions
public Task SaveAsync(Stream destination, CsvSaveOptions options = null, CancellationToken cancellationToken = null) #TaskAsynchronously saves the document to a path.
Parameters
- path System.String
- options OfficeIMO.CSV.CsvSaveOptions = null
- cancellationToken System.Threading.CancellationToken = null
SaveAsync(System.IO.Stream destination, OfficeIMO.CSV.CsvSaveOptions options, System.Threading.CancellationToken cancellationToken) #Asynchronously saves the document to a caller-owned writable stream.
Parameters
- destination System.IO.Stream
- options OfficeIMO.CSV.CsvSaveOptions
- cancellationToken System.Threading.CancellationToken
public static Void SaveObjects(String path, IEnumerable<Object> items, CsvSaveOptions options = null) #VoidSaves a sequence of objects directly as CSV without materializing a CsvDocument.
Parameters
- path System.String
- Destination CSV path.
- items System.Collections.Generic.IEnumerable{System.Object}
- Sequence of objects to convert into CSV rows.
- options OfficeIMO.CSV.CsvSaveOptions = null
- Optional save settings.
public CsvDocument SortBy<TKey>(Func<CsvRow, TKey> keySelector, Boolean descending = false, IComparer<TKey> comparer = null) #CsvDocumentSorts rows by a column name.
Type Parameters
- TKey
Parameters
- columnName System.String
- descending System.Boolean = false
- comparer System.Collections.Generic.IComparer{System.Object} = null
SortBy``1(System.Func{OfficeIMO.CSV.CsvRow,``0} keySelector, System.Boolean descending, System.Collections.Generic.IComparer{``0} comparer) #Sorts rows using a typed key selector.
Parameters
- keySelector System.Func{OfficeIMO.CSV.CsvRow,``0}
- descending System.Boolean
- comparer System.Collections.Generic.IComparer{``0}
public Byte[] ToBytes(CsvSaveOptions options = null) #Byte[]Encodes the document using the selected CSV encoding and compression.
Parameters
- options OfficeIMO.CSV.CsvSaveOptions = null
public DataTable ToDataTable(CsvDataTableOptions options = null) #DataTableProjects the CSV document into a DataTable.
Parameters
- options OfficeIMO.CSV.CsvDataTableOptions = null
- DataTable projection options. When omitted, all columns are emitted as strings.
Returns
A DataTable containing all document rows.
public MemoryStream ToStream(CsvSaveOptions options = null) #MemoryStreamEncodes the document in a new writable memory stream positioned at the beginning.
Parameters
- options OfficeIMO.CSV.CsvSaveOptions = null
public String ToString(CsvSaveOptions options) #StringSerializes the document to a string using the provided save options.
Parameters
- options OfficeIMO.CSV.CsvSaveOptions
public CsvDocument Transform(Func<CsvDocument, CsvDocument> transformer) #CsvDocumentExecutes a custom transformation.
Parameters
- transformer System.Func{OfficeIMO.CSV.CsvDocument,OfficeIMO.CSV.CsvDocument}
public CsvDocument Validate(out IReadOnlyList<CsvValidationError> errors) #CsvDocumentValidates the document against the configured schema.
Parameters
- errors System.Collections.Generic.IReadOnlyList{OfficeIMO.CSV.CsvValidationError}@
public CsvDocument ValidateOrThrow() #CsvDocumentValidates and throws when validation fails.
public CsvDocument WithCulture(CultureInfo culture) #CsvDocumentSets the culture for type conversions.
Parameters
- culture System.Globalization.CultureInfo
public CsvDocument WithDateTimeFormats(params String[] formats) #CsvDocumentSets additional date/time formats used by typed row conversion and schema validation.
Parameters
- formats System.String[]
public CsvDocument WithDelimiter(Char delimiter) #CsvDocumentSets the delimiter used for reading and writing.
Parameters
- delimiter System.Char
public CsvDocument WithEncoding(Encoding encoding) #CsvDocumentSets the encoding used for file operations.
Parameters
- encoding System.Text.Encoding
public CsvDocument WithHeader(params String[] headers) #CsvDocumentReplaces the header row.
Parameters
- headers System.String[]
public static Void WriteDataReader(TextWriter writer, IDataReader reader, CsvSaveOptions options = null) #VoidWrites an IDataReader directly as CSV without materializing a CsvDocument.
Parameters
- writer System.IO.TextWriter
- Destination text writer.
- reader System.Data.IDataReader
- Source data reader positioned before the first row.
- options OfficeIMO.CSV.CsvSaveOptions = null
- Optional save settings.
public static Void WriteObjects(TextWriter writer, IEnumerable<Object> items, CsvSaveOptions options = null) #VoidWrites a sequence of objects directly as CSV without materializing a CsvDocument.
Parameters
- writer System.IO.TextWriter
- Destination text writer.
- items System.Collections.Generic.IEnumerable{System.Object}
- Sequence of objects to convert into CSV rows.
- options OfficeIMO.CSV.CsvSaveOptions = null
- Optional save settings.
Inherited Methods
Properties
public IReadOnlyList<String> Header { get; } #Gets the document header columns.
public Char Delimiter { get; } #Gets the delimiter used by the document.
public CultureInfo Culture { get; } #Gets the culture used for type conversions.
public Encoding Encoding { get; } #Gets the encoding used when reading or writing files.
public IReadOnlyList<String> DateTimeFormats { get; } #Gets the configured date/time formats used by typed conversions.
public CsvLoadMode Mode { get; } #Gets the load mode of the document.
Extension Methods
public static IEnumerable<T> Map<T>(CsvDocument document, Action<CsvMapper<T>> configure) #IEnumerable<T>Type Parameters
- T
Parameters
- document CsvDocument
- configure Action<CsvMapper<T>>