Generate EDI files

Create an instance of X12Writer for writing X12 or HIPAA documents or EdifactWriter for writing EDIFACT or EANCOM documents.

The writer needs a stream or a file to write to. It is up to the consumer to preserve the sequence of what is being written. There is no need to write any of the 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 interchanges, groups and messages can be written to the same stream or file. Writing an interchange header always begins a new interchange and 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.

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)
:, +, ?, *, '

The writer will automatically apply an UNA segment for EDIFACT documents if the custom separator set is different than the default.

Please refer to the SDK for full usage and samples.

using (var stream = new MemoryStream())
{
    var writer = new X12Writer(stream);
}
var writer = new X12Writer(@"C:\output.edi", true);
using (var stream = new MemoryStream())
{
    var writer = new X12Writer(stream, , Encoding.Default, "\n");
}
using (var stream = new MemoryStream())
{
    var writer = new X12Writer(stream, , Encoding.Default, "", true);
}
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();
}
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                            
}