Getting Started with EDIFACTWriter
Requirements:
EDISDK
Introduction
The EDIFACTWriter component provides a straightforward way to generate EDIFACT messages programmatically. Designed to be intuitive and flexible, it simplfies common tasks, like building envelope headers and creating messages.
This guide walks through the basics of creating a simple EDIFACT document, as well as how to handle validation.
Tip: Download the latest version of EDISDK to follow along.
Step 1: Set Up
The EDIFACTWriter component is highly flexible, and its configuration largely depends on intended use. The default settings are sufficient for many cases, the component will write to an in-memory string and use standard delimiters and encoding. However, if a change in behavior is desired, certain settings must be applied before starting to write to ensure consistency throughout the document.
Key settings to configure before writing include:
- Output mode options (OutputFile, OutputData, or SetOutputStream)
- File write mode options (overwrite vs. append)
- Encoding options (e.g., iso-8859-1)
- Segment delimiters and suffixes
Setting these before writing begins ensures that all segments and messages use the same conventions consistently.
Step 2: Handling Headers
Headers in the EDIFACTWriter component are human-readable and intuitive. Each level of an EDIFACT document has its own set of header fields. Refer to the type definitions in the help documentation for a complete list of fields. The example below demonstrates the correct order in which headers should be opened:
Interchange -> Functional Group -> Message.
// Interchange headers
writer.Interchange.SyntaxIdentifier = "UNOB";
writer.Interchange.SenderIdentification = "ACME";
writer.Interchange.RecipientIdentification = "WAYNE_TECH";
writer.Interchange.ReferenceNumber = "000244";
writer.Interchange.ApplicationReference = "INVOICE";
writer.StartInterchange();
// Functional Group headers (Functional Groups are optional)
writer.FunctionalGroup.ReferenceNumber = "219088";
writer.FunctionalGroup.GroupIdentification = "Invoice";
writer.FunctionalGroup.AppSenderIdentification = "Sender";
writer.FunctionalGroup.AppRecipientIdentification = "Receiver";
writer.StartFunctionalGroup();
// Message headers
writer.Message.MessageType = "INVOIC";
writer.Message.MessageVersion = "D";
writer.Message.MessageRelease = "97A";
writer.StartMessage();
Note: Header fields are applied the next time the corresponding start method is called. To ensure a header is generated with the desired values, set its fields before calling StartInterchange, StartFunctionalGroup, or StartMessage. Fields set afterward will be applied the next time a header of that type is generated.
Step 3: Writing Segments
After a message header has been started, segments form the body of the message. The component does not enforce schema rules directly, leaving that responsibility to the application. This design provides the flexibility to target any schema version or custom variation, while still ensuring that valid EDIFACT output is produced when schema rules are followed.
The process is straightforward: begin a segment with StartSegment, write elements or composites, and then close the segment with EndSegment. Composite elements follow the same pattern, open with StartCompositeElement, write the components, and close with EndCompositeElement. If an element or component needs to be skipped, simply write an empty value in its place.
writer.StartSegment("BGM");
writer.WriteElement("351");
writer.WriteElement("");
writer.WriteElement("9");
writer.EndSegment();
writer.StartSegment("DTM");
writer.StartCompositeElement();
writer.WriteComponent("137");
writer.WriteComponent("201404192036");
writer.WriteComponent("203");
writer.EndCompositeElement();
writer.EndSegment();
Step 4: Creating Footers
After setting up headers and writing message segments, call EndMessage to close the message. Similarly, use EndFunctionalGroup and EndInterchange to close the associated section. EDIFACTWriter handles all trailers automatically, so it is easy to include multiple messages, functional groups, or interchanges within a single transmission. Once the transmission is complete, call Close to ensure all resources are released.
Step 5: Validating Writer Output (Optional)
If required, the application can validate the output using EDIFACTValidator component. Set the EDIFACTValidator's InputFile to the EDIFACTWriters’s output and handle events for schema resolution and validation warnings. An example on how that might be accomplished is included below:
EDIFACTValidator validator = new EDIFACTValidator();
validator.InputFile = edifactwriter.OutputFile;
validator.OnResolveSchema += (s, e) =>
{
validator.LoadSchema(SchemaPath, e.MessageType);
};
// Handle validation warnings in real time or review them using the ValidationErrors collection.
validator.OnValidateWarning += (s, e) =>
{
Console.WriteLine($"Validation Error: [{e.Position}-{e.Line},{e.Column}] {e.ErrorCode}: {e.ErrorMessage}");
};
// Run validation
validator.Validate();
The validation will check for missing or out-of-order segments, invalid data formats, and incorrect code list values. Handling warnings and errors through events allows immediate response to issues.
Tip: Run validation before sending a transmission to catch errors early and ensure messages comply with the standard.
Conclusion
The EDIFACTWriter and EDIFACTValidator components provide a clear and reliable way to generate and verify EDIFACT documents. With human-readable headers, flexible segment management, and schema-based validation, they enable applications to produce structured business data that meets required standards efficiently.
Whether creating a single message, multiple messages within functional groups, or full transmissions across multiple interchanges, EDIFACTWriter simplifies building and finalizing EDIFACT documents. Using EDIFACTValidator ensures that messages conform to schemas, catching missing segments, invalid codes, or incorrect formats before transmission.
By following the steps in this guide, developers can quickly get up and running, automating EDIFACT message creation and compliance.
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.