Skip to content

Reading EDI and Writing JSON or XML

olmelabs edited this page Nov 29, 2017 · 1 revision

Reading EDI and Writing JSON or XML

using EdiEngine;
using EdiEngine.Runtime;
using Newtonsoft.Json;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string edi =
                @"ISA*01*0000000000*01*0000000000*ZZ*ABCDEFGHIJKLMNO*ZZ*123456789012345*101127*1719*U*00400*000003438*0*P*>
                GS*OW*7705551212*3111350000*20000128*0557*3317*T*004010
                ST*940*0001
                W05*N*538686**001001*538686
                LX*1
                W01*12*CA*000100000010*VN*000100*UC*DEC0199******19991205
                G69*11.500 STRUD BLUBRY
                W76*56*500*LB*24*CF
                SE*7*0001
                GE*1*3317
                IEA*1*000003438";

            EdiDataReader r = new EdiDataReader();
            EdiBatch b = r.FromString(edi);

            //Serialize the whole batch to JSON
            JsonDataWriter w1 = new JsonDataWriter();
            string json = w1.WriteToString(b);

            //OR Serialize selected EDI message to Json
            string jsonTrans = JsonConvert.SerializeObject(b.Interchanges[0].Groups[0].Transactions[0]);
            
            //Serialize the whole batch to XML
            XmlDataWriter w2 = new XmlDataWriter();
            string xml = w2.WriteToString(b);
        }
    }
}