Article author
  • Updated

Parsing and generating EDI from/to CSV

EDI Tools for .NET is a .NET library that allows you to:

  • Read the contents of a CSV file into POCOs, which are instances of EDI templates.
  • Write the contents of POCOs into EDI files.

Learn How to represent any model using EDI templates article which also applicable to CSV files.

Before you can read and write CSV files with EDI Tools for .NET you need to manually create an EDI template that matches the structure of your custom CSV files. 

All concepts available in the Parse Flat Files and Generate Flat Files articles are valid for CSV files as well because CSV is a subset of flat files and contains delimited lines.


Create EDI Template for custom CSV

Let's assume that we have a CSV file that looks like this (including column headers):

001002003,555666411,10,15,EA,10000234500,20170520,SHIPTO NAME,123456,SHIPTO ADDRESS1,SHIPTOCITY,OH,43200
002003004,555666817,20,20,EA,10000234500,20170520,SHIPTO NAME,123456,SHIPTO ADDRESS1,SHIPTOCITY,OH,43200
003004005,555666908,10,10,EA,10000234500,20170520,SHIPTO NAME,123456,SHIPTO ADDRESS1,SHIPTOCITY,OH,43200
004005006,555666321,10,15,EA,10000234500,20170520,SHIPTO NAME,123456,SHIPTO ADDRESS1,SHIPTOCITY,OH,43200
005006007,555666287,10,20,EA,10000234500,20170520,SHIPTO NAME,123456,SHIPTO ADDRESS1,SHIPTOCITY,OH,43200
006007008,555666413,20,1,EA,10000234500,20170520,SHIPTO NAME,123456,SHIPTO ADDRESS1,SHIPTOCITY,OH,43200

The full example is available in GitHub (Run4 method):

This file can easily be represented by the following EDI template:

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

namespace EdiFabric.Examples.X12.MapEDI
    [Message("Flat", "PurchaseOrder")]
    public class PurchaseOrder : EdiMessage
        public Header Header { get; set; }

        public List<Item> Items { get; set; }       

    [Segment("LINE_NUMBER", '\0')]
    public class Header
        public string Data { get; set; }

    [Segment("", ',')]
    public class Item
        public string LineNumber { get; set; }
        public string UPCNumber { get; set; }
        public string Quantity { get; set; }
        public string Price { get; set; }
        public string UOM { get; set; }
        public string PONumber { get; set; }
        public string PODate { get; set; }
        public string CustomerName { get; set; }
        public string CustomerNumber { get; set; }
        public string AddressLine { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostCode { get; set; }

Separate segments are defined for the header and the repeating items. The header has a tag "LINE_NUMBER" to be identified with and no delimiter, e.g. all data will be added to the same field "Data".

The Item segment has no tag, e.g. all lines after the header will be appended to the items list, and defines comma "," as the delimiter. The order of fields needs to match the order in the CSV file.


Read CSV file into an instance of the EDI template

Stream ediStream = File.OpenRead(@"\..\..\..\Files\CSV_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())
private static MessageContext FlatFactory(string segment)
    var id = segment.Substring(0, 2);
    switch (id)
        case "LI":
	    return new MessageContext("PurchaseOrder", "Flat", mc => Assembly.Load(new AssemblyName("EdiFabric.Examples.FlatFile.Common")));

    return null;


Additional information:

Share this:

Was this article helpful?



Please sign in to leave a comment.