Class CsvReader
Represents a reader designed for CSV text. It is capable of deserializing objects from individual lines of CSV text, transforming CSV lines of text into objects, or simply reading the lines of CSV as an array of strings.
Namespace: Swan.Formatters
Syntax
public class CsvReader : IDisposable
Examples
The following example describes how to load a list of objects from a CSV file.
using Swan.Formatters;
class Example
{
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
static void Main()
{
// load records from a CSV file
var loadedRecords =
CsvReader.LoadRecords<Person>("C:\\Users\\user\\Documents\\file.csv");
// loadedRecords =
// [
// { Age = 20, Name = "George" }
// { Age = 18, Name = "Juan" }
// ]
}
}
The following code explains how to read a CSV formatted string.
using Swan.Formatters;
using System.Text;
using Swan.Formatters;
class Example
{
static void Main()
{
// data to be read
var data = @"Company,OpenPositions,MainTechnology,Revenue
Co,2,""C#, MySQL, JavaScript, HTML5 and CSS3"",500
Ca,2,""C#, MySQL, JavaScript, HTML5 and CSS3"",600";
using(var stream = new MemoryStream(Encoding.UTF8.GetBytes(data)))
{
// create a CSV reader
var reader = new CsvReader(stream, false, Encoding.UTF8);
}
}
}
Constructors
CsvReader(Stream)
Initializes a new instance of the CsvReader class. It automatically closes the stream when disposing this reader and uses the Windows 1253 encoding.
Declaration
public CsvReader(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | The stream. |
CsvReader(Stream, Encoding)
Initializes a new instance of the CsvReader class. It will automatically close the stream upon disposing.
Declaration
public CsvReader(Stream stream, Encoding textEncoding)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | The stream. |
Encoding | textEncoding | The text encoding. |
CsvReader(Stream, Boolean, Encoding)
Initializes a new instance of the CsvReader class.
Declaration
public CsvReader(Stream inputStream, bool leaveOpen, Encoding textEncoding)
Parameters
Type | Name | Description |
---|---|---|
Stream | inputStream | The stream. |
Boolean | leaveOpen | if set to |
Encoding | textEncoding | The text encoding. |
CsvReader(String)
Initializes a new instance of the CsvReader class. It uses the Windows 1252 Encoding by default and it automatically closes the file when this reader is disposed of.
Declaration
public CsvReader(string filename)
Parameters
Type | Name | Description |
---|---|---|
String | filename | The filename. |
CsvReader(String, Encoding)
Initializes a new instance of the CsvReader class. It automatically closes the file when disposing this reader.
Declaration
public CsvReader(string filename, Encoding encoding)
Parameters
Type | Name | Description |
---|---|---|
String | filename | The filename. |
Encoding | encoding | The encoding. |
Properties
Count
Gets number of lines that have been read, including the headings.
Declaration
public ulong Count { get; }
Property Value
Type | Description |
---|---|
UInt64 | The count. |
EndOfStream
Gets a value indicating whether the stream reader is at the end of the stream In other words, if no more data can be read, this will be set to true.
Declaration
public bool EndOfStream { get; }
Property Value
Type | Description |
---|---|
Boolean |
|
EscapeCharacter
Gets or sets the escape character. By default it is the double quote '"'.
Declaration
public char EscapeCharacter { get; set; }
Property Value
Type | Description |
---|---|
Char | The escape character. |
SeparatorCharacter
Gets or sets the separator character. By default it is the comma character ','.
Declaration
public char SeparatorCharacter { get; set; }
Property Value
Type | Description |
---|---|
Char | The separator character. |
Methods
Dispose()
Declaration
public void Dispose()
Dispose(Boolean)
Releases unmanaged and - optionally - managed resources.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
Boolean | disposing |
|
LoadRecords<T>(Stream)
Loads the records from the stream This method uses Windows 1252 encoding.
Declaration
public static IList<T> LoadRecords<T>(Stream stream)
where T : new()
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | The stream. |
Returns
Type | Description |
---|---|
IList<T> | A generic collection of objects that can be individually accessed by index. |
Type Parameters
Name | Description |
---|---|
T | The type of IList items to load. |
LoadRecords<T>(String)
Loads the records from the give file path. This method uses Windows 1252 encoding.
Declaration
public static IList<T> LoadRecords<T>(string filePath)
where T : new()
Parameters
Type | Name | Description |
---|---|---|
String | filePath | The file path. |
Returns
Type | Description |
---|---|
IList<T> | A generic collection of objects that can be individually accessed by index. |
Type Parameters
Name | Description |
---|---|
T | The type of IList items to load. |
ReadHeadings()
Reads a line of CSV text and stores the values read as a representation of the column names to be used for parsing objects. You have to call this method before calling ReadObject methods.
Declaration
public string[] ReadHeadings()
Returns
Type | Description |
---|---|
String[] | An array of the specified element type containing copies of the elements of the ArrayList. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Reading headings is only supported as the first read operation. or ReadHeadings. |
EndOfStreamException | Cannot read past the end of the stream. |
ReadLine()
Reads a line of CSV text into an array of strings.
Declaration
public string[] ReadLine()
Returns
Type | Description |
---|---|
String[] | An array of the specified element type containing copies of the elements of the ArrayList. |
Exceptions
Type | Condition |
---|---|
EndOfStreamException | Cannot read past the end of the stream. |
ReadObject()
Reads a line of CSV text, converting it into a dynamic object The property names correspond to the names of the CSV headings.
Declaration
public IDictionary<string, object> ReadObject()
Returns
Type | Description |
---|---|
IDictionary<String, Object> | Object of the type of the elements in the collection of key/value pairs. |
ReadObject(IDictionary<String, String>)
Reads a line of CSV text, converting it into a dynamic object in which properties correspond to the names of the headings.
Declaration
public IDictionary<string, object> ReadObject(IDictionary<string, string> map)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<String, String> | map | The mappings between CSV headings (keys) and object properties (values). |
Returns
Type | Description |
---|---|
IDictionary<String, Object> | Object of the type of the elements in the collection of key/value pairs. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | ReadHeadings. |
EndOfStreamException | Cannot read past the end of the stream. |
ArgumentNullException | map. |
ReadObject<T>()
Reads a line of CSV text converting it into an object of the given type, and assuming the property names of the target type match the heading names of the file.
Declaration
public T ReadObject<T>()
where T : new()
Returns
Type | Description |
---|---|
T | The conversion of specific type of object. |
Type Parameters
Name | Description |
---|---|
T | The type of object. |
ReadObject<T>(IDictionary<String, String>)
Reads a line of CSV text converting it into an object of the given type, using a map (or Dictionary) where the keys are the names of the headings and the values are the names of the instance properties in the given Type.
Declaration
public T ReadObject<T>(IDictionary<string, string> map)
where T : new()
Parameters
Type | Name | Description |
---|---|---|
IDictionary<String, String> | map | The map of CSV headings (keys) and Type property names (values). |
Returns
Type | Description |
---|---|
T | The conversion of specific type of object. |
Type Parameters
Name | Description |
---|---|
T | The type of object to map. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | map. |
InvalidOperationException | ReadHeadings. |
EndOfStreamException | Cannot read past the end of the stream. |
ReadObject<T>(IDictionary<String, String>, ref T)
Reads a line of CSV text converting it into an object of the given type, using a map (or Dictionary) where the keys are the names of the headings and the values are the names of the instance properties in the given Type. The result object must be already instantiated.
Declaration
public void ReadObject<T>(IDictionary<string, string> map, ref T result)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<String, String> | map | The map. |
T | result | The result. |
Type Parameters
Name | Description |
---|---|
T | The type of object to map. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | map or result. |
InvalidOperationException | ReadHeadings. |
EndOfStreamException | Cannot read past the end of the stream. |
SkipRecord()
Skips a line of CSV text. This operation does not increment the Count property and it is useful when you need to read the headings skipping over a few lines as Reading headings is only supported as the first read operation (i.e. while count is still 0).
Declaration
public void SkipRecord()
Exceptions
Type | Condition |
---|---|
EndOfStreamException | Cannot read past the end of the stream. |