server info
server info

Reputation: 1335

C# Library for ASCII File import/mapping

this is maybe a dump question but I give it a try.

One of a common task is to import data from ascii files. It's almost always the same beside the structure of the file. Comma separated, line seperated, take 5 rows, take 12... whatever... So it's always a different protocol/mapping but the same handling...

Is there a library for c# which helps to support this day-to-day scenario?

Upvotes: 4

Views: 2202

Answers (3)

Jaroslav Jandek
Jaroslav Jandek

Reputation: 9563

So the only thing those tasks have in common is reading a text file?

If FileHelpers is overkill for you (simple text data, etc.), standard .NET classes should be all you need (String.Split Method, Regex Class, StreamReader Class).

They provide reading delimited by characters (String.Split) or lines (StreamReader).

Upvotes: 1

Mikael Östberg
Mikael Östberg

Reputation: 17166

This is awesome: FileHelpers Library

Example:

File:

1732,Juan Perez,435.00,11-05-2002 
554,Pedro Gomez,12342.30,06-02-2004 
112,Ramiro Politti,0.00,01-02-2000 
924,Pablo Ramirez,3321.30,24-11-2002 

Create a class that maps your data.

[DelimitedRecord(",")]
public class Customer
{
    public int CustId;

    public string Name;

    public decimal Balance;

    [FieldConverter(ConverterKind.Date, "dd-MM-yyyy")]
    public DateTime AddedDate;

}

And then parse using:

FileHelperEngine engine = new FileHelperEngine(typeof(Customer));

// To Read Use:
Customer[] res = engine.ReadFile("FileIn.txt") as Customer[];

// To Write Use:
engine.WriteFile("FileOut.txt", res);

Enumerate:

foreach (Customer cust in res)
{
   Console.WriteLine("Customer Info:");
   Console.WriteLine(cust.Name + " - " +
                  cust.AddedDate.ToString("dd/MM/yy"));
}

Upvotes: 4

madd0
madd0

Reputation: 9323

You may want to take a look at the FileHelpers library.

Upvotes: 1

Related Questions