Class Table
A table.
public class Table
- Inheritance
-
Table
- Inherited Members
Constructors
Table()
Initializes a new instance of the Table class.
public Table()
Table(TextReader, bool)
Initializes a new instance of the Table class.
public Table(TextReader reader, bool hasHeaderRow)
Parameters
reader
TextReaderThe text reader from which to read the CSV table data.
hasHeaderRow
boolWhether the first row in the table is a header row.
Remarks
Blank lines are ignored, but lines containing only whitespace will be parsed as rows with a single field containing the whitespace.
Exceptions
- ArgumentNullException
reader
is null.- Exception
An error occurred while parsing the data.
Table(string, bool)
Initializes a new instance of the Table class.
public Table(string csv, bool hasHeaderRow)
Parameters
csv
stringThe CSV text from which to initialize the table data.
hasHeaderRow
boolWhether the first row in the table is a header row.
Remarks
Blank lines are ignored, but lines containing only whitespace will be parsed as rows with a single field containing the whitespace.
Exceptions
- ArgumentNullException
csv
is null.- Exception
An error occurred while parsing the data.
Properties
ColumnCount
Gets the column count.
public int ColumnCount { get; }
Property Value
Remarks
Each row (and header row) in the table can have different field counts. This value will reflect the row (or header row) with the most fields. Some rows (or header row) could have fewer fields than this value.
Headers
Gets or sets the header row, or null if there is no header row.
public string[] Headers { get; set; }
Property Value
- string[]
Rows
Gets the list of rows.
public IList<string[]> Rows { get; }
Property Value
Methods
EnumerateHeaderAndRows()
Enumerates all rows in the table, including the header row if one exists.
public IEnumerable<string[]> EnumerateHeaderAndRows()
Returns
- IEnumerable<string[]>
An enumerable collection of rows, starting with the header row if one exists.
FindHeaderIndex(string)
Finds the index of a column for a given header.
public int FindHeaderIndex(string header)
Parameters
header
stringThe column header.
Returns
- int
The index of the matching column, or -1 if none matched.
Remarks
Column headers are loosely matched, disregarding case, white space, and underscores.
Exceptions
- InvalidOperationException
The table does not contain a header row.
- InvalidOperationException
Multiple column headers in the table match.
Import(Stream, string, bool)
Imports a table from a file stream.
public static Table Import(Stream fileStream, string fileName, bool hasHeaderRow)
Parameters
fileStream
StreamThe file stream from which to read the table data.
fileName
stringThe file name, which is only used to look at the file extension to determine the type of source file.
hasHeaderRow
boolWhether the first row in the table is a header row.
Returns
Exceptions
- ArgumentNullException
fileStream
is null.- ArgumentNullException
fileName
is null.- InvalidOperationException
The file type is not supported.
- Exception
Some other error occurred while reading or parsing the data. The possible list of exceptions depends on which importer is being used.
Import(string, bool)
Imports a table from a file.
public static Table Import(string path, bool hasHeaderRow)
Parameters
path
stringA relative or absolute path for the file from which to read the table data.
hasHeaderRow
boolWhether the first row in the table is a header row.
Returns
Exceptions
- ArgumentNullException
path
is null.- InvalidOperationException
The file type is not supported.
- Exception
Some other error occurred while reading or parsing the data. The possible list of exceptions depends on which importer is being used.
IsColumnEmpty(int)
Gets whether a column is empty.
public bool IsColumnEmpty(int columnIndex)
Parameters
columnIndex
intThe column index.
Returns
Exceptions
- ArgumentOutOfRangeException
columnIndex
is less than zero or greater than or equal to ColumnCount.
IsEquivalentTo(Table)
Compares this table content to another.
public bool IsEquivalentTo(Table other)
Parameters
other
TableThe other table to compare.
Returns
IsFieldEmpty(string)
Gets whether a field is empty.
public static bool IsFieldEmpty(string field)
Parameters
field
stringA field value.
Returns
IsRowEmpty(string[])
Gets whether a row is empty.
public static bool IsRowEmpty(string[] row)
Parameters
row
string[]A row.
Returns
Exceptions
- ArgumentNullException
row
is null.
RemoveColumn(int)
Removes a column from the table.
public void RemoveColumn(int columnIndex)
Parameters
columnIndex
intThe column index.
Exceptions
- ArgumentOutOfRangeException
columnIndex
is less than zero or greater than or equal to ColumnCount.
RemoveEmptyColumns()
Removes all empty columns from the table.
public void RemoveEmptyColumns()
Remarks
For a column to be empty, the field in that column in the header and in all rows must be empty.
RemoveEmptyRows()
Removes all empty rows from the table.
public void RemoveEmptyRows()
Remarks
For a row to be empty, every field in the row must be empty. The header, if it exists, will not be changed, even if it is empty.
ToCsv()
Produces the table data as a CSV-formatted string.
public string ToCsv()
Returns
- string
The table data as a CSV-formatted string.
TruncateColumns(int)
Truncates columns from the table.
public void TruncateColumns(int columnCount)
Parameters
columnCount
intThe new column count.
Remarks
If columnCount
is greather than or equal to ColumnCount, the table will not be changed.
Exceptions
- ArgumentOutOfRangeException
columnCount
is less than zero.