Flat files (CSV, VDA, etc.) files translation

Flat files (CSV, VDA) Parsing with FlatReader

EdiFabric translates flat files and VDA files into C# instances of the corresponding template class.

FlatReader provides fast, non-cached, forward-only access to flat file data. FlatReader expects the full contents of the file to be represented with a single template, e.g. to not have batches of the root template. The flat reader expects the contents to be represented as a sequence of lines, each line ending with a new line (CR and LF, CR or LF). Each line can be either positional (of varying length) or delimited (including nested items) with a delimiter. Flat files can contain a mixture of delimited and positional lines.

The reader accepts two parameters - 1. StreamReader, to read the file from, 2. Type, the type of the template to instantiate.

Stream ediStream = File.OpenRead(@"C:\\Flat_PO.txt");

using (var streamReader = new StreamReader(ediStream, Encoding.UTF8, true, 1024))
{
	using (var csvReader = new FlatReader(streamReader, typeof(FlatPO)))
	{
		var result = csvReader.ReadToEnd() as FlatPO;
	}
}

Check out the full example solutions here:

 

 

 


 

 

 

Flat file template guidelines

 

  • Lines with tags

Lines starting with tags, must be set to segment with id equal to that of the tag, e.g. the following line:

PO2019-12-17

with tag PO, should be represented as:

[Segment("PO")]
public class Header

 

  • Lines without tags

Lines starting without a tag, must be set to segment with empty id. The should also have lines with tags before and after them, e.g. the following line:

US David Pastrnak 100 Legends Way Boston MA 02114

which has no flag as the first values can differ (country code), should be represented as:

[Segment("")]
public class Customer

and located between lines with tags:

[Message("Flat", "PO")]
public class FlatPO : EdiMessage
{
	[Required]
	[Pos(1)]
	public Header Header { get; set; }

	[Required]
	[Pos(2)]
	public List<Customer> Customers { get; set; }

	[Required]
	[Pos(3)]
	public Items Items { get; set; }        
}

 

  • Positional lines

Positional lines are represented as segments with data elements in a specified order, each annotated with [StringLength] attribute where Min and Max are equal.

The following line:

US David Pastrnak 100 Legends Way Boston MA 02114

can be represented as:

[Segment("")]
public class Customer
{
	[Required]
	[StringLength(10, 10, false, ' ')]
	[Pos(1)]
	public string Country { get; set; }

	[Required]
	[StringLength(20, 20, false, ' ')]
	[Pos(2)]
	public string FullName { get; set; }

	[Required]
	[StringLength(20, 20, false, ' ')]
	[Pos(3)]
	public string Street { get; set; }

	[Required]
	[StringLength(15, 15, false, ' ')]
	[Pos(4)]
	public string City { get; set; }

	[Required]
	[StringLength(3, 3, false, ' ')]
	[Pos(5)]
	public string State { get; set; }

	[Required]
	[StringLength(5, 5, false, ' ')]
	[Pos(6)]
	public string PostCode { get; set; }
}

 

  • Delimited lines

Delimited lines are represented as segments with data elements (including composite data elements), specifying the delimiter (and composite delimiter):

The following line:

ITEMS,ITEM213-CC|Bauer Nexus 2N Pro|1|168.95|Hockey stick,ITEM323-CC|CCM RibCor 70K|1|500.98|Hockey skates

can be represented as:

[Serializable()]
    [Segment("ITEMS", ',', '|')]
    public class Items
    {

        [Pos(1)]
        public string Tag { get; set; }

        [Pos(2)]
        public List<ItemDetail> ItemDetails { get; set; }
    }

    [Serializable()]
    [Composite("ItemDetails")]
    public class ItemDetail
    {
        [Pos(1)]
        public string ProductCode { get; set; }

        [Pos(2)]
        public string Description { get; set; }

        [Pos(3)]
        public string Quantity { get; set; }

        [Pos(4)]
        public string UnitPrice { get; set; }

        [Pos(5)]
        public string Notes { get; set; }
    }
Was this article helpful?
1 out of 3 found this helpful