Write EDI documents

Creating EDI documents from scratch allows you to exchange EDI with any trading partner that is EDI-compliant.

To create an EDI message you start by instantiating an object of the EDI Spec class you need. The values are populated according to the spec just like a regular .NET object. You don't have to build any of the trailers as this will be done automatically by the framework and your values will be discarded.

Combine messages of the same type in a single functional group. Groups are mandatory for X12 and not mandatory (and are rarely used) for Edifact. Refer to your specification if groups are required when dealing with Edifact.

The interchange header contains values such as the sender and receiver codes and qualifiers (which should have been agreed between the partners), time stamp and control number. Please note that the combination of sender code (ISA06) + receiver code (ISA08) + control number (ISA13) should be unique. Trading partners will detect duplicates based on this combination and your document may be rejected.

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 write an EDI document you need a stream or a file to write to. The writer can output interchange headers, group headers messages and custom segments. 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.

The writer also allows for a custom string to be added to the stream and expects this to be a valid segment.

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.

There are two writers available - X12Writer for writing X12 or Hipaa documents and EdifactWriter for writing Edifact or Eancom documents.

The constructor takes either the stream to write to or a file path, pointing to the file to write to and whether to append or overwrite.

Create writer with stream

using (var stream = new MemoryStream())
{
    var writer = new X12Writer(stream);
}

Create writer with file path

var writer = new X12Writer(@"C:\output.edi", true);

Create with segment postfix, which will be appended after each segment

using (var stream = new MemoryStream())
{
    var writer = new X12Writer(stream, , Encoding.Default, "\n");
}

Situational segments and blanks can also be written to the output by populating the relevant data elements with empty strings and then setting the PreserveWhitespace flag.

Create with PreserveWhitespace flag

using (var stream = new MemoryStream())
{
    var writer = new X12Writer(stream, , Encoding.Default, "", true);
}

To create EDI documents start by writing the interchange header first, followed by any group headers and finally messages. It's entirely up to the consumer to preserve the order of groups and messages, and to also ensure that only messages of the same type are written after a group header.

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.

Writing a message will append it to the group or interchange that was last started. You can append as many messages as you want and in any order.

To complete the writing call Flush, which clears the internal buffer to ensure all data is properly sent to the destination.

Write to stream

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 the second message ...
    writer.Write(m810_2);    

    writer.Flush();
}

The writer always uses the default separators per standard (see above). To apply a custom set of separators, write the interchange header with a new separator set.

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

Write to stream with custom separators

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 contain the custom separators                            
}