Project Description
A flat file parser capable of loading in complete or partial flat text files. It will convert each row in the file into a standard CLR object.

Non-String types such as int, float, datetime are supported, even complex types can be parsed.

Usage

The library is designed to be as simple-to-use as possible. All that you need to do is parse a file is to create a new type, and to set attributes for how that type will be processed.

Here is an example file layout:

Field Type Length
ID int 4
Product Name string 17
Amount float 6
Quantity Available int 5

And for that layout, here is a sample file:

0001Bicycle          299.9900010
0002Horn               5.9900143
0003Wheel             25.9900055
0004Seat              15.7500030

Step 1

The first step to parsing this file is to convert the layout into a class. Here is the basic class.

 

public class ProductList
{
    public int ID { get; set; }
    public string Name { get; set; }
    public float Amount { get; set; }
    public int Quantity { get; set; }
}

 

Step 2

We have created properties (not fields!) that are public and have getters and setters available. For the second step, let's expand on that by first telling the flat file processor that this class is intended to process a full line in the text file, as opposed to a partial one. This makes some things easier that we'll get into later. I've bolded and italicized the new attribute below.

 

[ProcessorType(LineProcessingType.Full)]
public class ProductList
{
    public int ID { get; set; }
    public string Name { get; set; }
    public float Amount { get; set; }
    public int Quantity { get; set; }
}

 

Adding that attribute to our class automatically lets the processor we'll use later know what processing type to execute.

Step 3

Now, for each property, we need to add a "FullProcessorField" attribute which explains in what order the fields come and their width in the flat file. The FullProcessorField attribute looks like this: [FullProcessorField(int order, int length)]

For the third step, Let's fill out all of the fields in our class.

 

[ProcessorType(LineProcessingType.Full)]
public class ProductList
{
    [FullProcessorField(1, 4)]
    public int ID { get; set; }

    [FullProcessorField(2, 17)]
    public string Name { get; set; }

    [FullProcessorField(3, 6)]
    public float Amount { get; set; }

    [FullProcessorField(4, 5)]
    public int Quantity { get; set; }
}

 

As you can see, the order increments with each field, and the length is set to the length described in our file specification.

Step 4

Lastly, we're going to process the file. Simply instantiate an object of type FlatFileProcessor<ProductList> and call the ProcessFile method.

var processor = new FileProcessor<ProductList>();
var results = processor.ProcessFile("ProductList.txt");

// results is an IEnumerable<ProductList>
foreach (var r in results)
{
    // r is of type ProductList.
    // do what you wish with the records!

    Console.WriteLine(r.Name + " is worth " + r.Amount.ToString("c"));
}

Last edited Jan 2, 2011 at 2:27 AM by DaveAndrews, version 3