Class CsvTableReader
Used to read a CSV table that includes row headers.
Namespace: Neon.Csv
Assembly: Neon.Common.dll
Syntax
public class CsvTableReader
Remarks
This class makes it easy to process tabular data loaded from a CSV file, where the first row of the file contains the row header strings that identify the table columns.
Initialize an instance by passing a CsvReader, stream, string, or file path to the constructor. The constructor will read the first row of the file and initialize the ColumnMap dictionary which maps the case insensitive column name to the zero based index of the column in the table.
You'll process each data row by calling ReadRow(). This returns a list
with the next row of data or null
if the end of the table has been reached. You can
process the row data returned directly or use the GetColumn(string) method to
access a column value on the current row directly.
note
This class is tolerant of blank or duplicate column names. In the case of duplicates, the first column matching the requested column name will be used when parsing data.
Applications should call the reader's Dispose() or Close() method when they are finished with the reader so that the underlying CsvReader will be closed as well, promptly releasing any system resources (such as the stream).
Constructors
CsvTableReader(CsvReader)
Constructs an instance to read from a CsvReader.
Declaration
public CsvTableReader(CsvReader reader)
Parameters
Type | Name | Description |
---|---|---|
CsvReader | reader | The CsvReader to read from. |
CsvTableReader(Stream, Encoding)
Constructs an instance to read from a stream.
Declaration
public CsvTableReader(Stream stream, Encoding encoding)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | The input stream. |
Encoding | encoding | The stream's character Encoding. |
CsvTableReader(TextReader)
Constructs an instance to read from a TextReader.
Declaration
public CsvTableReader(TextReader reader)
Parameters
Type | Name | Description |
---|---|---|
TextReader | reader | The reader. |
CsvTableReader(string)
Constructs an instance to read from a CSV string.
Declaration
public CsvTableReader(string text)
Parameters
Type | Name | Description |
---|---|---|
string | text | The CSV text. |
Properties
ColumnMap
Returns the dictionary that case insensitvely maps a column name to the zero based index of the column.
Declaration
public Dictionary<string, int> ColumnMap { get; }
Property Value
Type | Description |
---|---|
Dictionary<string, int> |
Columns
Returns the list of table columns in the order read from the source.
Declaration
public List<string> Columns { get; }
Property Value
Type | Description |
---|---|
List<string> |
this[int]
Indexer that returns the value for a column.
Declaration
public string this[int column] { get; }
Parameters
Type | Name | Description |
---|---|---|
int | column | The column index. |
Property Value
Type | Description |
---|---|
string | The column value or |
this[string]
Indexer that returns the value for the named column in the current row.
Declaration
public string this[string columnName] { get; }
Parameters
Type | Name | Description |
---|---|---|
string | columnName | The column name. |
Property Value
Type | Description |
---|---|
string | The column value or |
Reader
Returns the underlying CsvReader or null
if the reader is closed.
Declaration
public CsvReader Reader { get; }
Property Value
Type | Description |
---|---|
CsvReader |
Methods
Close()
Closes the reader if it is still open.
Declaration
public void Close()
Dispose()
Releases any system resources held by the instance,
Declaration
public void Dispose()
GetColumn(string)
Returns the value for the named column in the current row.
Declaration
public string GetColumn(string columnName)
Parameters
Type | Name | Description |
---|---|---|
string | columnName | The column name. |
Returns
Type | Description |
---|---|
string | The column value or |
IsEmpty(string)
Determines whether a cell in a named column in the current row is empty or if the column does not exist.
Declaration
public bool IsEmpty(string columnName)
Parameters
Type | Name | Description |
---|---|---|
string | columnName | The column name. |
Returns
Type | Description |
---|---|
bool |
|
ReadRow()
Reads the next row of table.
Declaration
public List<string> ReadRow()
Returns
Type | Description |
---|---|
List<string> | The list of column values or |
Rows()
Returns an enumerator that returns the data rows from a CsvTableReader.
Declaration
public IEnumerable<List<string>> Rows()
Returns
Type | Description |
---|---|
IEnumerable<List<string>> | The next row as a List<T>. |