Parse and Generate Custom Flat Files Last updated: 2026-01-23

ediFabric .NET C# Examples

The example below is part of the ediFabric .NET C# Code Examples.

Download EDI C# Examples

The Parse Custom Flat File code is also available on GitHub

Additional operations for Flat Files

How to parse custom Flat File with ediFabric .NET

using EdiFabric.Framework.Readers;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using EdiFabric.Core.Model.Edi;
using System.Collections.Generic;
using EdiFabric.Framework;

namespace EdiFabric.Examples.FlatFile.Read
{
    class ReadCSVFile
    {
        public static void Run()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

            Stream ediStream = File.OpenRead(Directory.GetCurrentDirectory() + @"\..\..\..\Files\Flat_PO.txt");
            List<IEdiItem> items = new List<IEdiItem>();

            using (StreamReader streamReader = new StreamReader(ediStream, Encoding.UTF8, true, 1024))
            {
                using (var flatReader = new FlatReader(streamReader, FlatFactory))
                {
                    while (flatReader.Read())
                    {
                        items.Add(flatReader.Item);
                    }
                }
            }
        }

        private static MessageContext FlatFactory(string segment)
        {
            var id = segment.Substring(0, 2);
            switch (id)
            {
                case "PO":
                    return new MessageContext("PO", "Flat", mc => Assembly.Load(new AssemblyName("EdiFabric.Examples.FlatFile.Common")));
                case "H,":
                    return new MessageContext("Markers", "Flat", mc => Assembly.Load(new AssemblyName("EdiFabric.Examples.FlatFile.Common")));
            }

            return null;
        }
    }
}

How to generate custom Flat File with ediFabric .NET

using EdiFabric.Examples.FlatFile.Common;
using EdiFabric.Framework.Writers;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;

namespace EdiFabric.Examples.FlatFile.Write
{
    class WriteCSVFile
    {
        public static void Run()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

            using (var stream = new MemoryStream())
            {
                using (var writer = new StreamWriter(stream, Encoding.UTF8, 4096, true))
                {
                    using (var flatWriter = new FlatWriter(writer, null))
                    {
                        var flatPO = POBuilder.BuildFlatPO();
                        flatWriter.Write(flatPO);
                    }

                    Debug.Write(stream.LoadToString());
                }
            }
        }
    }
}
using System.Collections.Generic;

namespace EdiFabric.Examples.FlatFile.Common
{
    public class POBuilder
    {
        public static FlatPO BuildFlatPO()
        {
            var result = new FlatPO();

            //  Header
            result.Header = new Header();
            result.Header.Tag = "PO";
            result.Header.Date = "2019-12-17";

            //  Customers
            result.Customers = new List<Customer>();

            //  Customer 1
            var customer1 = new Customer();
            customer1.FullName = "David Pastrnak";
            customer1.Street = "100 Legends Way";
            customer1.City = "Boston";
            customer1.PostCode = "02114";
            customer1.State = "MA";
            customer1.Country = "US";
            result.Customers.Add(customer1);

            //  Customer 2
            var customer2 = new Customer();
            customer2.FullName = "Nathan MacKinnon";
            customer2.Street = "1000 Chopper Cir";
            customer2.City = "Denver";
            customer2.PostCode = "80204";
            customer2.State = "CO";
            customer2.Country = "US";
            result.Customers.Add(customer2);

            //  Items
            result.Items = new Items();
            result.Items.Tag = "ITEMS";
            result.Items.ItemDetails = new List<ItemDetail>();

            //  Item 1
            var item1 = new ItemDetail();
            item1.Description = "Bauer Nexus 2N Pro";
            item1.ProductCode = "ITEM213-CC";
            item1.Quantity = "1";
            item1.UnitPrice = "168.95";
            item1.Notes = "Hockey stick";
            result.Items.ItemDetails.Add(item1);

            //  Item 2
            var item2 = new ItemDetail();
            item2.Description = "CCM RibCor 70K";
            item2.ProductCode = "ITEM323-CC";
            item2.Quantity = "1";
            item2.UnitPrice = "500.98";
            item2.Notes = "Hockey skates";
            result.Items.ItemDetails.Add(item2);

            return result;
        }
    }
}

Flat Files

ediFabric .NET can parse and generate flat files of any structure, e.g. positional, delimited, hierarchical, or a mixture of them. The structure of flat files is represented by EDI templates.

Custom Flat File Code Example

Flat-file code example is available at:

Custom Flat File Template

ediFabric .NET uses C# classes to represent the layout of EDI transactions. These classes are nothing more than standard C# .NET classes with some extra markup and are referred to as EDI templates.

Template for Custom Flat File

using System;
using System.Collections.Generic;
using EdiFabric.Core.Annotations.Edi;
using EdiFabric.Core.Annotations.Validation;
using EdiFabric.Core.Model.Edi;

namespace EdiFabric.Templates.Vda
{
    [Serializable()]
    [Message("Flat", "PO")]
    public class FlatPO : EdiMessage
    {
        [Required]
        [Pos(1)]
        public Header Header { get; set; }

        [Splitter]
        [Required]
        [Pos(2)]
        public List<Customer> Customers { get; set; }

        [Required]
        [Pos(3)]
        public Items Items { get; set; }
    }

    [Serializable()]
    [Segment("PO")]
    public class Header
    {
        [Required]
        [StringLength(2, 2)]
        [Pos(1)]
        public string Tag { get; set; }

        [Required]
        [StringLength(10, 10)]
        [Pos(2)]
        public string Date { get; set; }
    }

    [Serializable()]
    [Segment("")]
    public class Customer
    {
        [Required]
        [StringLength(10, 10, false, ' ')]
        [Pos(1)]
        public string Country { get; set; }

        [Required]
        [StringLength(20, 20)]
        [Pos(2)]
        public string FullName { get; set; }

        [Required]
        [StringLength(20, 20)]
        [Pos(3)]
        public string Street { get; set; }

        [Required]
        [StringLength(15, 15)]
        [Pos(4)]
        public string City { get; set; }

        [Required]
        [StringLength(3, 3, false, ' ')]
        [Pos(5)]
        public string State { get; set; }

        [Required]
        [StringLength(5, 5, false, ' ')]
        [Pos(6)]
        public string PostCode { get; set; }
    }

    [Serializable()]
    [Segment("ITEMS", ',', '|')]
    public class Items
    {

        [Pos(1)]
        public string Tag { get; set; }

        [Pos(2)]
        public List<ItemDetail> ItemDetails { get; set; }
    }

    [Serializable()]
    [Composite("ItemDetails")]
    public class ItemDetail
    {
        [Pos(1)]
        public string ProductCode { get; set; }

        [Pos(2)]
        public string Description { get; set; }

        [Pos(3)]
        public string Quantity { get; set; }

        [Pos(4)]
        public string UnitPrice { get; set; }

        [Pos(5)]
        public string Notes { get; set; }
    }
}

Parse your first Flat file in five minutes

The Community plan is free forever and needs no credit card. It includes ediFabric .NET, Native and Cloud with one serial key.

Sign up free Validate EDI files free