Home

Awesome

FlatMapper

Build status NuGet Version NuGet Downloads

FlatMapper is a library to import and export data from and to plain text files in a Linq compatible way.

Features

How to use

Fixed Length Layout

var layout = new Layout<TestObject>.FixedLengthLayout()
				.HeaderLines(1)
				.WithMember(o => o.Id, set => set.WithLength(5).WithLeftPadding('0'))
				.WithMember(o => o.Description, set => set.WithLength(25).WithRightPadding(' '))
				.WithMember(o => o.NullableInt, set => set.WithLength(5).AllowNull("=Null").WithLeftPadding('0'))
                .WithMember(o => o.NullableEnum, set => set.WithLength(10).AllowNull("======NULL").WithLeftPadding(' '))
                .WithMember(o => o.Date, set => set.WithLength(19).WithFormat(new CultureInfo("pt-PT"))); //PT-pt default dates are always fixed 19 chars "13-12-2015 23:41:41"

Delimited Layout

var layout = new Layout<TestObject>.DelimitedLayout()
	            .WithDelimiter(";")
	            .WithQuote("\"")
				.HeaderLines(1)
	            .WithMember(o => o.Id, set => set.WithLength(5).WithLeftPadding('0'))
	            .WithMember(o => o.Description, set => set.WithLength(25).WithRightPadding(' '))
	            .WithMember(o => o.NullableInt, set => set.WithLength(5).AllowNull("=Null").WithLeftPadding('0'))
                .WithMember(o => o.NullableEnum, set => set.WithLength(10).AllowNull("").WithLeftPadding(' '))
                .WithMember(o => o.Date, set => set.WithFormat(new CultureInfo("pt-PT")));

Reading and Writing

//Reading data
using (var fileStream = File.OpenRead("c:\temp\data.txt"))
{
    var flatfile = new FlatFile<TestObject>(layout, fileStream);
    foreach(var objectInstance in flatfile.Read())
    {
        //Do Somethig....
    }
}

//Writing data
using (var fileStream = File.OpenWrite("c:\temp\data.txt"))
{
    var flatfile = new FlatFile<TestObject>(layout, fileStream)
    flatfile.Write(listOfObjects);
}

For more detailed information please check the wiki