Skip to content

e7ffa9d9 0ef2 02e9 74eb 95b423660295

Axel Kesseler edited this page Aug 16, 2024 · 6 revisions

CsvImporter(TInstance) Class

This static class allows reading a value list of TInstance types either from a file or simply from a stream (which actually can also be a file).

Inheritance Hierarchy

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

Syntax

C#

public static class CsvImporter<TInstance>
where TInstance : class

Type Parameters

 

TInstance
The class type that fully describes the structure of the CSV input to be read.

Methods

 

Name Description
Private methodStatic member Construct This method constructs an instance of type TInstance by calling its standard constructors.
Private methodStatic member GetHeaderName This method returns the expected header name of a single column.
Private methodStatic member IsHeaderLine This method tries to determine whether given list of cells contain all and only header names.
Private methodStatic member IsHeaderName The method evaluates whether give name equals to the header name set in the descriptor.
Public methodStatic member Load(Stream) This method tries to load all values from given stream.
Public methodStatic member Load(String) This method tries to load all values from given file.
Public methodStatic member Load(Stream, CsvSettings) This method tries to load all values from given stream using given settings.
Public methodStatic member Load(String, CsvSettings) This method tries to load all values from given file using given settings.
Private methodStatic member ProcessLine This method tries to process given line according to given set of parameters.
Private methodStatic member ValidateColumns This method validates the number of columns of each line.
Private methodStatic member ValidateHeader This method tries to validate given headers by applying exact validation rules.
  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

Examples

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

using Plexdata.CsvParser.Attributes;
using Plexdata.CsvParser.Processors;
using System;
using System.Collections.Generic;
using System.Globalization;

namespace MyCsvImporter
{
    class Program
    {
        [CsvDocument]
        class CsvCustomer
        {           
            [CsvIgnore]
            public Int32 Id { get; set; }

            [CsvColumn(Offset = 2, Header = "Identifier")]
            public Int32 ExternalId { get; set; }

            [CsvColumn(Offset = 1, Header = "Forename")]
            public String FirstName { get; set; }

            [CsvColumn(Offset = 0, Header = "Surname")]
            public String LastName { get; set; }

            [CsvColumn(Offset = 5, Header = "Active")]
            public Boolean IsActive { get; set; }

            [CsvColumn(Offset = 3, Header = "Date")]
            public DateTime? EntryDate { get; set; }

            [CsvColumn(Offset = 4, Header = "Sales")]
            public Decimal SalesAverage { get; set; }

            [CsvColumn(Offset = 6, Header = "Notes")]
            public String Description { get; set; }
        }

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

                CsvSettings settings = new CsvSettings
                {
                    Culture = CultureInfo.GetCultureInfo("en-US"),
                    Mappings = new CsvMappings
                    {
                        TrueValues = new List<String> { "yeah" },
                        FalseValues = new List<String> { "nope" },
                    },
                };

                // Source file could contain this content:
                // Surname,  Forename,    Identifier, Date,       Sales,      Active, Notes
                // "Marley", "Bob",       1001,       2007-05-03, "1,234.56", nope,   "Have a short note here."
                // "Monroe", "Marilyn",   1002,       2008-06-05, "1,234.56", nope,   ""
                // "Snipes", "Wesley",    1003,       2009-07-06, "1,234.56", yeah,   "Have a short note here." 
                // "Hurley", "Elizabeth", 1004,       2005-08-08, "1,234.56", yeah,   "Have a short note here."

                IEnumerable<CsvCustomer> result = CsvImporter<CsvCustomer>.Load(filename, settings);

                foreach (CsvCustomer current in result)
                {
                    Console.WriteLine(current.ToString());
                }

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

See Also

Reference

Plexdata.CsvParser.Processors Namespace

Clone this wiki locally