EDI Generator

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.

Upon completion Flush needs to be called as the final step. 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.

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

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

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.

X12

using (var stream = new MemoryStream())
{
    var writer = new X12Writer(stream);
    
    var isa = new ISA();  
    // construct the interchange header ...
                                    
    var newSeparators = new Separators(
        '\n', 
        Separators.X12.ComponentDataElement, 
        Separators.X12.DataElement, 
        null, 
        null);  

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

EDIFACT

using (var stream = new MemoryStream())
{
    var writer = new EdifactWriter(stream);
    
    var unb = new UNB();  
    // construct the interchange header ...
                                    
    var newSeparators = new Separators(
        '\n', 
        Separators.Edifact.ComponentDataElement, 
        Separators.Edifact.DataElement, 
        null, 
        null);  

    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.

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())
{
    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);    

    writer.Flush();
}

EDIFACT

using (var stream = new MemoryStream())
{
    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);    

    writer.Flush();
}

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.