Generating flat files (CSV, VDA) with Flat Writer
Generating a flat-file with EDI Tools for .NET is a three-step process:
- Create an instance of the required EDI template (or C# class), e.g. TS4905 for a VDA delivery instruction.
- Populate that instance with data, usually, this requires custom code to convert either a database/domain object or export from an ERP in CSV, XML, JSON, etc. format, to the EDI template.
- Write the instance to a flat file or a stream using Flat writer.
The red path below depicts the generation of a flat-file:
- A .NET object (instance of its EDI template)
- Is processed through a Flat writer (EdiFabric)
- To produce a flat file
How to create and populate .NET objects
Head on to our Generate EDI file article to learn how to populate the C# POCOs. The concepts in the article are applicable to flat files as well.
How to write flat objects
The purpose of FlatWriter is to write an already populated C# instance of a particular EDI template, to a text flat file or stream.
FlatWriter provides a fast, non-cached, forward-only way to generate streams or files. It is up to the consumer to preserve the sequence of what is being written.
FlatWriter implements IDisposable and needs to be disposed of. This ensures that all buffered data is properly written and the internal buffer is cleared.
using (var stream = new MemoryStream())
{
using (var writer = new StreamWriter(stream, Encoding.UTF8, 4096, true))
{
using (var flatWriter = new FlatWriter(writer))
{
flatWriter.Write(BuildFlatPO());
}
}
}
Examples in GitHub:
Configuration of FlatWriter
FlatWriter can be configured with the following additional settings:
- postfix - the string to be appended after each line. By default it is Environment.NewLine.
Flat File Delimiters
FlatWriter applies the value of postfix as a line delimiter. When no value is specified, Environment.NewLine is used.
Guidelines for creating EDI templates
Although the templates represent flat files and not EDI files, we'll keep calling them EDI templates for the sake of consistency as they are essentially the same thing.
-
Lines beginning with tags must have the first field allocated for the tag.
[Segment("PO")] public class Header { [Required] [StringLength(2, 2)] [Pos(1)] public string Tag { get; set; } [Required] [StringLength(10, 10)] [Pos(2)] public string Date { get; set; } }
This line will be identified by the "PO" tag specified in the [Segment] attribute. The first property, string Tag, will always have the value of "PO".
-
Positional lines can have each field padded up to the specified Max length. This can be enabled in the [Segment] attribute where the second parameter controls the padding direction (left or right, right by default), and the third parameter is the padding character:
[Segment("511", false, ' ')]
This example will pad all fields in this line to their MaxLength with blanks ' ' to the right.
Padding can also be applied at the field level, which supersedes the setting at the [Segment] attribute:
[Required] [StringLength(10, 10, true, ' ')] [Pos(1)] public string Country { get; set; }
This example will pad the Country field with blanks, up to 10 positions, to the left.
Write VDA files
Although VDA files can be written using FlatWriter, there is a legacy VdaWriter that can also be used to write VDA files.
var deliveryInstruction = TS4905Builder.BuildDeliveryInstruction(22, 23); using (var stream = new MemoryStream()) { using (var writer = new VdaWriter(stream, Environment.NewLine)) { writer.Write(deliveryInstruction); } }
Examples in GitHub:
Comments
0 comments
Please sign in to leave a comment.