EDI File Generation

Generate EDI files with EDI Writer

EdiWriter provides a fast, non-cached, forward-only way to generate streams or files that contain EDI data. It is up to the consumer to preserve the sequence of what is being written. There is no need to write any of the EDI trailers because they are set automatically.

All writers implement IDisposable and need to be disposed of. This ensures that all buffered data is properly written and the internal buffer is cleared.

Multiple EDI interchanges, EDI groups or EDI transactions can be written to the same stream or file. Writing an interchange header always begins a new interchange and EdiFabric automatically applies an interchange trailer to the previous if any. Anything written after this point will be batched under the last interchange header.

Writing a group header follows the same concept, and to begin a new group simply write the group header. This will automatically apply a group trailer to the previous if any. All messages written after this point will be batched under the last group header.

Check out the full example solutions here:

 

 

 


 

 

 

EDI writers

To generate X12 or HIPAA EDI documents create an instance of X12Writer.

To generate EDIFACT or EANCOM EDI documents create an instance of EdifactWriter.

 

 

 


 

 

 

EDI Separators

If not explicitly specified the writer will use the default separators per standard:

X12

Component, DataElement, Escape, Repetition, Segment

>, *, null, ^, ~

EDIFACT

Component, DataElement, Escape, Repetition, Segment

:, +, ?, *, '

To change the default separators and generate EDI using custom separators use the Separators class and pass it when writing the EDI interchange header. Every segment or EDI transaction written after this point will use the new separators until a new interchange header is written.

NOTE:

If you want to change the default separators do not set them in the ISA or the UNA segment, construct a new Separators object and pass it on when writing the ISA out.

X12

using (var stream = new MemoryStream())
{
   using(var writer = new X12Writer(stream))
   {
       var isa = new ISA();  
       // construct the interchange header ...
                                    
       var newSeparators = Separators.X12;      
       // change the segment separator, the rest are the default for X12
       newSeparators.Segment = '\n';  

       writer.Write(isa, newSeparators); 
                                    
       // everything written after this point will use the custom separators
   }
}

EDIFACT

using (var stream = new MemoryStream())
{
   using(var writer = new EdifactWriter(stream))
   {    
       var unb = new UNB();  
       // construct the interchange header ...
                                    
       var newSeparators = Separators.Edifact;
       // change the segment separator, the rest are the default for X12
       newSeparators.Segment = '\n'; 

       writer.Write(unb, newSeparators); 
    
       // everything written after this point will use the custom separators
   }
}

 

 

 


 

 

 

EDI Generation

You need to manually create instances of the header control segments. EDI generation always begins with writing the interchange header (ISA or UNB). You can't generate EDI documents without a valid interchange header.

Quickstart: Explore the tutorials section for X12, HIPAA, EDIFACT, and EANCOM transaction generation examples.

Following the interchange header, you can start writing functional groups and EDI transactions in the desired sequence. It's up to the consumer to define the order and Microsoft's TPL can be utilized to perform the tasks asynchronously.

X12

using (var stream = new MemoryStream())
{
   using(var writer = new X12Writer(stream))
   {    
       var isa = new ISA();
       // construct the interchange header ...
       writer.Write(isa); 
                                       
       var gs = new GS();
       // construct the group header ...
       writer.Write(gs);  
                                      
       var m810_1 = new TS810();
       // construct the first message ...
       writer.Write(m810_1);

       var m810_2 = new TS810();
       // construct other messages ...
       writer.Write(m810_2);    
   }
}

EDIFACT

using (var stream = new MemoryStream())
{
   using(var writer = new EdifactWriter(stream))
   {    
       var unb = new UNB();
       // construct the interchange header ...
       writer.Write(unb); 
                                       
       var ung = new UNG();
       // construct the group header ...
       writer.Write(ung);  
                                      
       var invoic_1 = new TSINVOIC();
       // construct the first message ...
       writer.Write(invoic_1);

       var invoic_2 = new TSINVOIC();
       // construct other messages ...
       writer.Write(invoic_2);    
   }
}

EdiWriter allows for postfixes to be automatically applied at the end of each segment. Blank spaces can also be preserved to output EDI data element exclusions.

 

 

 


 

 

 

EDI Generation Async

X12

using (var stream = new MemoryStream())
{
   using(var writer = new X12Writer(stream))
   {    
       var isa = new ISA();
       // construct the interchange header ...
       await writer.WriteAsync(isa); 
                                       
       var gs = new GS();
       // construct the group header ...
       await writer.WriteAsync(gs);  
                                      
       var m810_1 = new TS810();
       // construct the first message ...
       await writer.WriteAsync(m810_1);

       var m810_2 = new TS810();
       // construct other messages ...
       await writer.WriteAsync(m810_2);    
   }
}

EDIFACT

using (var stream = new MemoryStream())
{
   using(var writer = new EdifactWriter(stream))
   {    
       var unb = new UNB();
       // construct the interchange header ...
       await writer.WriteAsync(unb); 
                                       
       var ung = new UNG();
       // construct the group header ...
       await writer.WriteAsync(ung);  
                                      
       var invoic_1 = new TSINVOIC();
       // construct the first message ...
       await writer.WriteAsync(invoic_1);

       var invoic_2 = new TSINVOIC();
       // construct other messages ...
       await writer.WriteAsync(invoic_2);    
   }
}

 

 

 


 

 

 

Write transactions only, without interchange or group envelopes

Messages can be written without having them wrapped in envelopes. The only requirement is to manually supply the separators to the writer.

X12

var settings = new X12WriterSettings() { Separators = Separators.X12 }
using (var stream = new MemoryStream())
{
   using(var writer = new X12Writer(stream, settings))
   {                                                
       var m810_1 = new TS810();
       // construct the first message ...
       await writer.WriteAsync(m810_1);

       var m810_2 = new TS810();
       // construct other messages ...
       await writer.WriteAsync(m810_2);    
   }
}

EDIFACT

var settings = new EdifactWriterSettings() { Separators = Separators.Edifact}
using (var stream = new MemoryStream())
{
   using(var writer = new EdifactWriter(stream, settings))
   {                                         
       var invoic_1 = new TSINVOIC();
       // construct the first message ...
       await writer.WriteAsync(invoic_1);

       var invoic_2 = new TSINVOIC();
       // construct other messages ...
       await writer.WriteAsync(invoic_2);    
   }
}

 

 

 


 

 

 

Turning the auto trailers off

In situations where auto trailers are not needed, for example when writing to a file at different times of the day, auto trailers can be turned off. There are two modes for that - transaction trailers and interchange\group trailers.

Transactions

Set the autoTrailer argument of Write to false.

using (var stream = new MemoryStream())
{
   using(var writer = new EdifactWriter(stream))
   {                                         
       var invoic_1 = new TSINVOIC();
       
       // construct the first message ...
       await writer.WriteAsync(invoic_1, false);    
   }
}

Interchanges and groups

Set the AutoTrailers flag of X12WriterSettings or EdifactWriterSettings to false.

var settings = new X12WriterSettings() { AutoTrailers = false  }
using (var stream = new MemoryStream())
{ 
    using(var writer = new X12Writer(stream, settings))     
    { 
        var isa = new ISA(); 
        // construct the interchange header ... 
        await writer.WriteAsync(isa); 

        var gs = new GS(); 
        // construct the group header ... 
        await writer.WriteAsync(gs); 

        var m810_1 = new TS810(); 
        // construct the first message ... 
        await writer.WriteAsync(m810_1);
    } 

    // no trailers are written 
}

 

 

 


 

 

 

Writing without X12Writer or EdifactWriter

EDI files can be produced without using the built-in X12 and EDIFACT writers at all by using the ToEdi() method for transactions and control segments. The ToEdi() method needs a separator set to output the transaction or control segment as a string. Examples below use StreamWriter.

X12

var settings = new X12WriterSettings() { Separators = Separators.X12 }
using (var stream = new MemoryStream()) { using(var writer = new StreamWriter(stream)) { var isa = new ISA(); // construct the interchange header ... writer.Write(isa.ToEdi(Separators.X12)); var gs = new GS(); // construct the group header ... writer.Write(gs.ToEdi(Separators.X12)); var m810 = new TS810(); // construct the first message ... writer.Write(m810.ToEdi(settings));

var ge = new GE(); // construct the group trailer ... writer.Write(ge.ToEdi(Separators.X12));

var iea = new IEA(); // construct the interchange trailer ... writer.Write(iea.ToEdi(Separators.X12));

writer.Flush(); } }

EDIFACT

var settings = new EdifactWriterSettings() { Separators = Separators.Edifact}
using (var stream = new MemoryStream()) { using(var writer = new StreamWriter(stream)) {
writer.Write(Separators.Edifact.ToUna());
var unb = new UNB(); // construct the interchange header ... writer.Write(unb.ToEdi(Separators.Edifact)); var invoic = new TSINVOIC(); // construct the first message ... writer.Write(invoic.ToEdi(settings));

var unz = new UNZ(); // construct the interchange trailer ... writer.Write(unz.ToEdi(Separators.Edifact));

writer.Flush(); } }
Was this article helpful?
3 out of 8 found this helpful