TxtCsvHelper 1.3.3

.NET Standard 2.0
Install-Package TxtCsvHelper -Version 1.3.3
dotnet add package TxtCsvHelper --version 1.3.3
<PackageReference Include="TxtCsvHelper" Version="1.3.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TxtCsvHelper --version 1.3.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TxtCsvHelper, 1.3.3"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install TxtCsvHelper as a Cake Addin
#addin nuget:?package=TxtCsvHelper&version=1.3.3

// Install TxtCsvHelper as a Cake Tool
#tool nuget:?package=TxtCsvHelper&version=1.3.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

TxtCsvHelper

TxtCsvHelper is a library to assist in parsing CSV, or any other form of delimited files. You can fill models, fill dynamics, or simply get a list of fields by line.

Installation

Use the package manager console to install TxtCsvHelper.

Install-Package TxtCsvHelper -Version 1.3.3

Usage

using TxtCsvHelper;

To return an IEnumerable<T>. Dynamic types must be IEnumerable<dynamic> Parser can take a StreamReader, Stream, or a string Delimiter if no stream exists (not for use with deserialize or Read) followed by 2 optional parameters: the delimiter string (will default to a comma). A bool if there is a header line (will default to true). Scroll to end for examples

//if you need to change the LineBreak from default Environment.NewLine - Configuration.NewLine = Some string
using(Parser pars = new Parser(streamvar, delimiter: ',', hasHeader: true))
{
          //if you want certain NumberStyles, CultureInfo, and/or DateTimeStyles - only necessary if you need custom conversion
         //Configuration.CultureInfo = Some Culture Info - will default to CultureInfo.CurrentCulture
         //Configuration.NumberStyles = Some Number Style - will default to NumberStyles.Curreny
        //Configuration.DateStyles = Some DateTime Style - will default to DateTimeStyles.AssumeLocal
	var models = pars.Deserialize<Person>();
}

To read line by line give parser a StreamReader, or a Stream. Set Delimiter to your delimiter if different than a comma. You can get fields in a variety of ways. Scroll to end for examples.

using (StreamReader rs = new StreamReader(postedFile.OpenReadStream()))
using(Parser parser = new Parser(sr, delimiter: ","))
{
	while(parser.Read())
	{
                List<string> substrings = parser.Substrings;
                string s = parser[0];
                string ss = parser.GetField(1);
                string sss = parser.GetField("FirstName);
                int ssss = parser.GetField<int>(3)
                int sssss = parser.GetField<int>("Age")
        }
}

To return an IEnumerable<string> for each line Parser in this case takes 2 optional parameters: the delimiter string (will default to a comma). A bool if there is a header line (will default to true). SplitLine takes a Line of delimited fields. Scroll to end for examples

using (StreamReader sr = new StreamReader(postedFile.OpenReadStream()))
using(Parser parser = new Parser(false, delimiter: ",", HasHeader: true))
{
	while(sr.Peek()>=0)
	{
                var n = parser.SplitLine(sr.ReadLine());
        }
}

If no header exists, you may declare the index of the field in the model with [SetIndex()] simply put the index starting with 0 inside the parentheses. You must set HasHeader to false to use SetIndex(). If a header exists but the names do not match your model, you may declare the header name with [HeaderName()] simply put the column name in the parentheses

public class Person
{
	[SetIndex(1)]
	[HeaderName(Last)]
        public string LastName { get; set; }
        [SetIndex(0)]
        public string FirstName { get; set; }
        [SetIndex(2)]
        public string MiddleName { get; set; }
        [SetIndex(3)]
        public int Age { get; set; }
}

Examples:

//Delimiter defaults to "," and HasHeader defaults to true. You can use .ReadAsync() with the same syntax
using(StreamReader sr = new StreamReader(postedFile.OpenReadStream()))
using(Parser parser = new Parser(sr))
{
     var models = parser.Deserialize<Person>();
}
//Delimiter defaults to "," and HasHeader defaults to true. You can use .ReadAsync() with the same syntax
//For long lines that do not contain line breaks in fields use .SplitLine() instead - see example at bottom
using(StreamReader sr = new StreamReader(postedFile.OpenReadStream()))
using(Parser parser = new Parser(sr, “\t”))
{
      //If you want to read the Header in and read line by line instead of .Deserialize
     parser.Read();
     //Must call this if you want to reference fields by column name
     parser.CacheHeaderRow();
     while(parser.Read())
     {
           string firstName = parser.GetField("FirstName");
           string lastName = parser.GetField("LastName");
           string middleName = parser.GetField("MiddleName");
           int age = parser.GetField<int>("Age");
      }
}
//Delimiter defaults to "," and HasHeader defaults to true. You can use .ReadAsync() with the same syntax
using(StreamReader sr = new StreamReader(postedFile.OpenReadStream()))
using(Parser parser = new Parser(sr))
{
      //If you want to read line by line. If you want to skip the first (or more) row call parser.Read() before the while loop.
     while(parser.Read())
     {
           string firstName = parser.GetField(0);
           string lastName = parser.GetField(1);
           string middleName = parser.GetField(2);
           int age = parser.GetField<int>(3);
      }
}
//Delimiter defaults to "," and HasHeader defaults to true. You can use .ReadAsync() with the same syntax
using(StreamReader sr = new StreamReader(postedFile.OpenReadStream()))
using(Parser parser = new Parser(sr))
{
      //If you want to read line by line. If you want to skip the first (or more) row call parser.Read() before the while loop.
     while(parser.Read())
     {
           string firstName = parser[0];
           string lastName = parser[1];
           string middleName = parser[2];
           if(int.TryParse(parser[3], out int i))
           {
                  int age = i;
           }
      }
}
//Delimiter defaults to "," and HasHeader defaults to true. You can use .ReadAsync() with the same syntax
using(StreamReader sr = new StreamReader(postedFile.OpenReadStream()))
using(Parser parser = new Parser(sr))
{
      //If you want to read line by line. If you want to skip the first (or more) row call parser.Read() before the while loop.
     while(parser.Read())
     {
          List<string> substrings = parser.Substrings;
          //do something with the substrings
      }
}
//Use this if you want split strings, you can give .SplitLine any string, it does not need to come from a StreamReader
//this is the most efficient method for long lines that do not contain line breaks in fields
//Delimiter defaults to "," and HasHeader defaults to true
using(StreamReader sr = new StreamReader(postedFile.OpenReadStream()))
using(Parser parser = new Parser(“,”)
{
       //if you want to skip the header row/rows call sr.ReadLine() before the loop
       while(sr.Peek()>=0)
       {
              var substrings = parser.SplitLine(sr.ReadLine());
              //do something with the substrings
       }
}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.3 75 6/24/2022
1.3.2 59 6/20/2022
1.3.1 389 8/2/2021
1.3.0 195 8/1/2021
1.2.9 171 7/12/2021
1.2.8 204 7/1/2021
1.2.7 189 6/21/2021
1.2.6 146 6/21/2021
1.2.5 171 5/17/2021
1.2.4 159 5/15/2021
1.2.3 153 5/11/2021
1.2.2 137 5/10/2021
1.2.1 145 5/9/2021
1.2.0 152 5/9/2021
1.1.9 161 5/9/2021
1.1.8 230 5/8/2021
1.1.7 163 5/8/2021
1.1.6 163 5/8/2021
1.1.5 205 5/7/2021
1.1.4 214 5/7/2021
1.1.3 209 5/7/2021
1.1.2 192 5/6/2021
1.1.1 175 5/6/2021
1.1.0 189 5/6/2021
1.0.1 176 5/6/2021
1.0.0 188 5/6/2021