Class TabularData
Provides static methods for working with collections of tabular data records and inferring dialects. This class cannot be inherited.
public static class TabularData
- Inheritance
-
TabularData
Methods
InferDialect(Stream, IEnumerable<string>, IEnumerable<char>, IEnumerable<char>, int, Encoding?)
Attempts to infer a dialect from the stream based on frequency of the eligible token values.
public static TabularDialect? InferDialect(Stream stream, IEnumerable<string> lineTerminators, IEnumerable<char> delimiters, IEnumerable<char> quoteSymbols, int sampleLength = 65536, Encoding? encoding = null)
Parameters
stream
StreamThe stream to infer from.
lineTerminators
IEnumerable<string>The eligible values for a line terminator.
delimiters
IEnumerable<char>The eligible values for a delimiter.
quoteSymbols
IEnumerable<char>The eligible values for a quote symbol.
sampleLength
intThe length of a sample in bytes.
encoding
EncodingThe encoding for reading from the stream.
Returns
- TabularDialect
A successfully inferred dialect or null.
Exceptions
- ArgumentException
stream
does not support reading.- ArgumentNullException
stream
,lineTerminators
,delimiters
, orquoteSymbols
is null.
InferDialectAsync(Stream, IEnumerable<string>, IEnumerable<char>, IEnumerable<char>, int, Encoding?, CancellationToken)
Asynchronously attempts to infer a dialect from the stream based on frequency of the eligible token values.
public static ValueTask<TabularDialect?> InferDialectAsync(Stream stream, IEnumerable<string> lineTerminators, IEnumerable<char> delimiters, IEnumerable<char> quoteSymbols, int sampleLength = 65536, Encoding? encoding = null, CancellationToken cancellationToken = default)
Parameters
stream
StreamThe stream to infer from.
lineTerminators
IEnumerable<string>The eligible values for a line terminator.
delimiters
IEnumerable<char>The eligible values for a delimiter.
quoteSymbols
IEnumerable<char>The eligible values for a quote symbol.
sampleLength
intThe length of a sample in bytes.
encoding
EncodingThe encoding for reading from the stream.
cancellationToken
CancellationTokenThe token to monitor for cancellation requests.
Returns
- ValueTask<TabularDialect>
A task object that, when awaited, produces a successfully inferred dialect or null.
Exceptions
- ArgumentException
stream
does not support reading.- ArgumentNullException
stream
,lineTerminators
,delimiters
, orquoteSymbols
is null.
ReadRecordsAsync<T>(Stream, TabularDialect, TabularOptions?, TabularHandler<T>?, int, int, CancellationToken)
Asynchronously reads all records from the stream that can be represented as T
.
public static ValueTask<T[]> ReadRecordsAsync<T>(Stream stream, TabularDialect dialect, TabularOptions? options = null, TabularHandler<T>? handler = null, int skip = 0, int take = 2147483647, CancellationToken cancellationToken = default) where T : notnull
Parameters
stream
StreamThe stream to read from.
dialect
TabularDialectThe dialect to use for reading.
options
TabularOptionsThe options to control the behavior during reading.
handler
TabularHandler<T>The handler to read a
T
instance from a record.skip
intThe number of records to skip before returning the remaining records.
take
intThe number of records to return.
cancellationToken
CancellationTokenThe token to monitor for cancellation requests.
Returns
- ValueTask<T[]>
A task object that, when awaited, produces an array of records.
Type Parameters
T
The type of an object that represents a record.
Exceptions
- ArgumentException
stream
does not support reading.- ArgumentNullException
stream
ordialect
is null.- InvalidOperationException
The record handler is not specified and cannot be found in the registry.
- OperationCanceledException
The cancellation token was canceled. This exception is stored into the returned task.
- TabularContentException
An unexpected character or end of stream was encountered.
ReadRecords<T>(Stream, TabularDialect, TabularOptions?, TabularHandler<T>?, int, int)
Reads all records that can be represented as T
from the stream.
public static T[] ReadRecords<T>(Stream stream, TabularDialect dialect, TabularOptions? options = null, TabularHandler<T>? handler = null, int skip = 0, int take = 2147483647) where T : notnull
Parameters
stream
StreamThe stream to read from.
dialect
TabularDialectThe dialect to use for reading.
options
TabularOptionsThe options to control the behavior during reading.
handler
TabularHandler<T>The handler to read a
T
instance from a record.skip
intThe number of records to skip before returning the remaining records.
take
intThe number of records to return.
Returns
- T[]
An array of records.
Type Parameters
T
The type of an object that represents a record.
Exceptions
- ArgumentException
stream
does not support reading.- ArgumentNullException
stream
ordialect
is null.- InvalidOperationException
The record handler is not specified and cannot be found in the registry.
- TabularContentException
An unexpected character or end of stream was encountered.
WriteRecordsAsync<T>(Stream, TabularDialect, IEnumerable<T>, TabularOptions?, TabularHandler<T>?, CancellationToken)
Asynchronously writes a collection of records to the stream.
public static ValueTask WriteRecordsAsync<T>(Stream stream, TabularDialect dialect, IEnumerable<T> records, TabularOptions? options = null, TabularHandler<T>? handler = null, CancellationToken cancellationToken = default) where T : notnull
Parameters
stream
StreamThe stream to write to.
dialect
TabularDialectThe dialect to use for writing.
records
IEnumerable<T>The records to write to the stream.
options
TabularOptionsThe options to control the behavior during writing.
handler
TabularHandler<T>The handler to write a
T
instance to a record.cancellationToken
CancellationTokenThe token to monitor for cancellation requests.
Returns
- ValueTask
A task object.
Type Parameters
T
The type of an object that represents a record.
Exceptions
- ArgumentException
stream
does not support writing.- ArgumentNullException
stream
,dialect
, orrecords
is null.- InvalidOperationException
The record handler is not specified and cannot be found in the registry.
- OperationCanceledException
The cancellation token was canceled. This exception is stored into the returned task.
WriteRecords<T>(Stream, TabularDialect, IEnumerable<T>, TabularOptions?, TabularHandler<T>?)
Writes a collection of records to the stream.
public static void WriteRecords<T>(Stream stream, TabularDialect dialect, IEnumerable<T> records, TabularOptions? options = null, TabularHandler<T>? handler = null) where T : notnull
Parameters
stream
StreamThe stream to write to.
dialect
TabularDialectThe dialect to use for writing.
records
IEnumerable<T>The records to write to the stream.
options
TabularOptionsThe options to control the behavior during writing.
handler
TabularHandler<T>The handler to write a
T
instance to a record.
Type Parameters
T
The type of an object that represents a record.
Exceptions
- ArgumentException
stream
does not support writing.- ArgumentNullException
stream
,dialect
, orrecords
is null.- InvalidOperationException
The record handler is not specified and cannot be found in the registry.