Parsing and generating EDI from/to CSV
EDI Tools for .NET is a .NET library that allows you to:
- Read the contents of a CSV file into POCOs, which are instances of EDI templates.
- Write the contents of POCOs into EDI files.
Learn How to represent any model using EDI templates article which also applicable to CSV files.
Before you can read and write CSV files with EDI Tools for .NET you need to manually create an EDI template that matches the structure of your custom CSV files.
All concepts available in the Parse Flat Files and Generate Flat Files articles are valid for CSV files as well because CSV is a subset of flat files and contains delimited lines.
Create EDI Template for custom CSV
Let's assume that we have a CSV file that looks like this (including column headers):
LINE_NUMBER,UPC_NUMBER,QUANTITY,PRICE,UOM,PO_NUMBER,PO_DATE,CUSTOMER_NAME,CUSTOMER_NUMBER,ADDRESS,CITY,STATE,POSTAL_CODE 001002003,555666411,10,15,EA,10000234500,20170520,SHIPTO NAME,123456,SHIPTO ADDRESS1,SHIPTOCITY,OH,43200 002003004,555666817,20,20,EA,10000234500,20170520,SHIPTO NAME,123456,SHIPTO ADDRESS1,SHIPTOCITY,OH,43200 003004005,555666908,10,10,EA,10000234500,20170520,SHIPTO NAME,123456,SHIPTO ADDRESS1,SHIPTOCITY,OH,43200 004005006,555666321,10,15,EA,10000234500,20170520,SHIPTO NAME,123456,SHIPTO ADDRESS1,SHIPTOCITY,OH,43200 005006007,555666287,10,20,EA,10000234500,20170520,SHIPTO NAME,123456,SHIPTO ADDRESS1,SHIPTOCITY,OH,43200 006007008,555666413,20,1,EA,10000234500,20170520,SHIPTO NAME,123456,SHIPTO ADDRESS1,SHIPTOCITY,OH,43200
The full example is available in GitHub (Run4 method):
This file can easily be represented by the following EDI template:
using EdiFabric.Core.Annotations.Edi; using EdiFabric.Core.Annotations.Validation; using EdiFabric.Core.Model.Edi; using System; using System.Collections.Generic; namespace EdiFabric.Examples.X12.MapEDI { [Serializable()] [Message("Flat", "PurchaseOrder")] public class PurchaseOrder : EdiMessage { [Required] [Pos(1)] public Header Header { get; set; } [Required] [Pos(2)] public List<Item> Items { get; set; } } [Serializable()] [Segment("LINE_NUMBER", '\0')] public class Header { [Required] [Pos(1)] public string Data { get; set; } } [Serializable()] [Segment("", ',')] public class Item { [Pos(1)] public string LineNumber { get; set; } [Pos(2)] public string UPCNumber { get; set; } [Pos(3)] public string Quantity { get; set; } [Pos(4)] public string Price { get; set; } [Pos(5)] public string UOM { get; set; } [Pos(6)] public string PONumber { get; set; } [Pos(7)] public string PODate { get; set; } [Pos(8)] public string CustomerName { get; set; } [Pos(9)] public string CustomerNumber { get; set; } [Pos(10)] public string AddressLine { get; set; } [Pos(11)] public string City { get; set; } [Pos(12)] public string State { get; set; } [Pos(13)] public string PostCode { get; set; } } }
Separate segments are defined for the header and the repeating items. The header has a tag "LINE_NUMBER" to be identified with and no delimiter, e.g. all data will be added to the same field "Data".
The Item segment has no tag, e.g. all lines after the header will be appended to the items list, and defines comma "," as the delimiter. The order of fields needs to match the order in the CSV file.
Read CSV file into an instance of the EDI template
Stream ediStream = File.OpenRead(@"\..\..\..\Files\CSV_PO.txt"); List<IEdiItem> items = new List<IEdiItem>(); using (StreamReader streamReader = new StreamReader(ediStream, Encoding.UTF8, true, 1024)) { using (var flatReader = new FlatReader(streamReader, FlatFactory)) { while (flatReader.Read()) { items.Add(flatReader.Item); } } }
private static MessageContext FlatFactory(string segment) { var id = segment.Substring(0, 2); switch (id) { case "LI": return new MessageContext("PurchaseOrder", "Flat", mc => Assembly.Load(new AssemblyName("EdiFabric.Examples.FlatFile.Common"))); } return null; }
Comments
0 comments
Please sign in to leave a comment.