Translate EDI files

Create an instance of X12Reader for reading X12 or HIPAA documents or EdifactReader for reading EDIFACT or EANCOM documents.

The reader needs the EDI stream to read from and the assembly (either its name or load factory that returns System.Assembly) that contains the class(es) of the transactions that are being read.

All readers implement IDisposable and should be disposed of either directly or indirectly.

The readers do not throw exceptions but instead return ReaderErrorContext as EdiItem when the EDI stream is corrupt and can't be read. All transactions are parsed until the reader can match segments according to the EDI rule. Upon reaching unrecognizable or improperly positioned segment, the ErrorContext of EdiMessage is populated with the relevant error details. Always check if messages were partially parsed before processing them downstream.

Please refer to the SDK for full usage and samples.

var ediStream = File.OpenRead(@"C:\edi.txt");
List<EdiItem> ediItems;
using(var reader = new X12Reader(ediStream, "EdiFabric.Rules.X12004010"))
    ediItems = reader.ReadToEnd().ToList();    
var ediStream = File.OpenRead(@"C:\edi.txt");
List<EdiItem> ediItems;
using(var reader = new X12Reader(ediStream, AssemblyLoadFactory))
    ediItems = reader.ReadToEnd().ToList(); 
                                
private static Assembly AssemblyLoadFactory(MessageContext messageContext)
{
    //  Resolve by sender
    if (messageContext.SenderId == "PartnerA")
        return Assembly.Load("EdiFabric.Rules.PartnerA");

    //  Resolve by version
    if (messageContext.Version == "004010")
        return Assembly.Load("EdiFabric.Rules.X12004010");

    throw new System.Exception(string.Format("Unsupported version {0}" messageContext.Version));
}
var ediStream = File.OpenRead(@"C:\edi.txt");
using(var reader = new X12Reader(ediStream, "EdiFabric.Rules.X12004010"))
{
    while (reader.Read())
    {
        ISA isa = ediReader.Item as ISA;
        if (isa != null)
        {
            //  Handle isa downstream
        }

        GS gs = ediReader.Item as GS;
        if (gs != null)
        {
            //  Handle gs downstream
        }

        TS810 invoice = ediReader.Item as TS810;
        if (invoice != null)
        {
            //  Handle invoice downstream
        }

        TS850 purchaseOrder = ediReader.Item as TS850;
        if (purchaseOrder != null)
        {
            //  Handle purchaseOrder downstream
        }
    }
}
var ediStream = File.OpenRead(@"C:\edi.txt");
List<EdiItem> ediItems;
using(var reader = new X12Reader(ediStream, "EdiFabric.Rules.X12004010"))
    ediItems = reader.ReadToEnd().ToList(); 
                                 
var readerErrors = ediItems.OfType<ReaderErrorContext>();
if (readerErrors.Any())
{
    //  The stream is corrupt. Reject it and report back to the sender
    foreach(var readerError in readerErrors)
    {
        //  Respond with the error context, which contains the standard EDI error code and fault reason
        var error = readerError.MessageErrorContext.Flatten();
    }
}
var ediStream = File.OpenRead(@"C:\edi.txt");
List<EdiItem> ediItems;
using(var reader = new X12Reader(ediStream, "EdiFabric.Rules.X12004010"))
    ediItems = reader.ReadToEnd().ToList(); 
                                 
var purchaseOrders = ediItems.OfType<TS850>();

foreach (var po in purchaseOrders)
{
    //  Check if partially parsed
    if (po.HasErrors)
    {
        //  Report it back to the sender, log, etc.
        var errors = po.ErrorContext.Flatten();
    }
    else
    {
        //  po is valid, handle it downstream
    }
}