2dc3c42e a6b1 46e3 8fd9 2288f528db87 - akesseler/Plexdata.CsvParser GitHub Wiki

CsvReader Class

This static class allows reading all values either from a file or simply from a stream (which actually can also be a file). The complete result is put into the data type CsvContainer, which serves as the container for the CSV content.

Inheritance Hierarchy

System.Object
  Plexdata.CsvParser.Processors.CsvReader
Namespace: Plexdata.CsvParser.Processors
Assembly: Plexdata.CsvParser.NET (in Plexdata.CsvParser.NET.dll) Version: 1.1.3+d5bef99aa35461ce4bafadf0bf2c2aeca18044ea

Syntax

C#

public static class CsvReader

Methods

 

Name Description
Private methodStatic member DumpLines Prints processed lines.
Public methodStatic member Read(Stream) This method tries to read all values from given stream.
Public methodStatic member Read(String) This method tries to read all values from given file.
Public methodStatic member Read(Stream, CsvSettings) This method tries to read all values from given stream using given settings.
Public methodStatic member Read(String, CsvSettings) This method tries to read all values from given file using given settings.
Private methodStatic member ReadLines Reads all lines from input stream.
  Back to Top

Remarks

CSV actually means Comma Separated Values. Sometimes it is also called as Character Separated Values. But no matter which name is used, CSV always represents a text file mainly used for data exchange between different system.

It would be possible (using a proper configuration) to read a CSV input according to the rules of RFC 4180. For more information about RFC 4180 please visit the web-site under https://www.ietf.org/rfc/rfc4180.txt

Actually, this class allows to read all CSV data as some kind of plain text. Such functionally seems to be necessary because sometimes CSV files may contain more or less data items as expected. Therefore, this class should be helpful to process CSV files with dynamic content.

Examples

This section wants to show a simple but hopefully useful example of how to use the CSV Parser to read data.

using Plexdata.CsvParser.Constants;
using Plexdata.CsvParser.Processors;
using System;

namespace MyCsvReader
{
    class Program
    {
        static void Main(String[] args)
        {
            try
            {
                String filename = @"C:\folder\file.csv";

                // Source file could contain this content:
                // Name;               Notes
                // "Marley, Bob";      "Jamaican singer-songwriter"
                // "Monroe, Marilyn";  "American actress";          "model and singer"
                // "Snipes, Wesley";   "American actor";            "director, film producer"; "martial artist"
                // "Hurley, Elizabeth" 

                CsvSettings settings = new CsvSettings() { Heading = true, Separator = ColumnSeparators.SemicolonSeparator };
                CsvContainer container = CsvReader.Read(filename, settings);

                String col0row1 = container.GetValue<String>(0, 1) as String; // Marley, Bob
                String col0row2 = container.GetValue<String>(0, 2) as String; // Monroe, Marilyn
                String col0row3 = container.GetValue<String>(0, 3) as String; // Snipes, Wesley
                String col0row4 = container.GetValue<String>(0, 4) as String; // Hurley, Elizabeth

                String col1row1 = container.GetValue<String>(1, 1) as String; // Jamaican singer-songwriter
                String col1row2 = container.GetValue<String>(1, 2) as String; // American actress
                String col1row3 = container.GetValue<String>(1, 3) as String; // American actor
                String col1row4 = container.GetValue<String>(1, 4) as String; // null

                String col2row1 = container.GetValue<String>(2, 1) as String; // null
                String col2row2 = container.GetValue<String>(2, 2) as String; // model and singer
                String col2row3 = container.GetValue<String>(2, 3) as String; // director, film producer
                String col2row4 = container.GetValue<String>(2, 4) as String; // null

                String col3row1 = container.GetValue<String>(3, 1) as String; // null
                String col3row2 = container.GetValue<String>(3, 2) as String; // null
                String col3row3 = container.GetValue<String>(3, 3) as String; // martial artist
                String col3row4 = container.GetValue<String>(3, 4) as String; // null

                Console.ReadKey();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
    }
}

See Also

Reference

Plexdata.CsvParser.Processors Namespace

⚠️ **GitHub.com Fallback** ⚠️