Creating An X12 EDI File

Index

    Creating An X12 EDI File

    Creating an EDI document is easy with the EDIDocument component.  Simply create an instance of the EDIDocument, set its loops, and call its GenerateFile() procedure.  EDIDocument is based on a loop hierarchy.  Loops contain segments and other loops. Segments contain elements. Elements contains data and composite elements.  For EDIFACT documents loops are the same as segment groups. 

    Example

    This example creates an EDIDocument instance, set its formation and call its GenerateFile() method.  We will create a simple X12 EDI file with the following loop structure:

     

    Implementation

    // Instantiate an instance of EDIDocument and set the name of the file to be created
    EDIDocument sampleEDIFile = new EDIDocument(“C:\\My837EDIFile.txt”);
     
     
    // Place the correct counts of segment, headers and functional groups
    sampleEDIFile.AutoPlaceCorrectNumOfSegments = true;
    sampleEDIFile.AutoPlaceCorrectNumOfST = true;
    sampleEDIFile.AutoPlaceCorrectNumOfGT = true;

    // Create an interchange loop. This loop will contain all other loops in the EDI structure
    Loop interchangeHeaderLoop = new Loop(“Interchange Header”);

    // Create an ISA segment in the interchange loop
    DataSegment isa = interchangeHeaderLoop.CreateSegment(“ISA”);

    // Add elements with data to the ISA segment
    isa.Elements.Add(“00”);
    isa.Elements.Add(“”);
    isa.Elements.Add(“00”);
    isa.Elements.Add(“”);
    isa.Elements.Add(“ZZ”);
    isa.Elements.Add(“InterchangeSenderID”);
    isa.Elements.Add(“ZZ”);
    isa.Elements.Add(“InterchangeReceiverID”);
    isa.Elements.Add(“070303”);
    isa.Elements.Add(“18:04”);
    isa.Elements.Add(“U”);
    isa.Elements.Add(“00401”);
    isa.Elements.Add(“1”);
    isa.Elements.Add(“1”);
    isa.Elements.Add(“T”);
    isa.Elements.Add(“:”);

    // Now create the function header loop
    Loop functionalHeaderLoop = interchangeHeaderLoop.CreateLoop(“Functional Header Loop”);

    // Add a GS segment to it
    DataSegment gs = functionalHeaderLoop.CreateSegment(“GS”);

    // Set its elements and their values
     
     
    gs.Elements.Add(“HC”);
    gs.Elements.Add(“ApplicationSenderCode”);
    gs.Elements.Add(“ApplicationReceiverCode”);
    gs.Elements.Add(“2005”);
    gs.Elements.Add(“132334”);
    gs.Elements.Add(“1”);
    gs.Elements.Add(“X”);
    gs.Elements.Add(“004010X098A1”);

    // Create a transaction header loop
    Loop transactionLoop = functionalHeaderLoop.CreateLoop(“Transaction Header”);

    // Add an ST segment to the transaction header loop
    DataSegment st =transactionLoop.CreateSegment(“ST”);

    // Set its elements and their values
    st.Elements.Add(“837”);
    st.Elements.Add(“1”);

    // Add an ST segment to the transaction header loop
    DataSegment bht = transactionLoop.CreateSegment(“BHT”);
    bht.Elements.Add(“0019”);
    bht.Elements.Add(“00”);
    bht.Elements.Add(“1”);
    bht.Elements.Add(“20070515”);
    bht.Elements.Add(“094553”);
    bht.Elements.Add(“CH”);

    // Create the end of the transaction (SE segment). This must be done in a loop.
    Loop endOfTransactionLoop = transactionLoop.CreateLoop(“End Of Transaction”);
    DataSegment se = endOfTransactionLoop.CreateSegment(“SE”);
    se.Elements.Add(DataSegment.NumberCreated.ToString(), “Segment Count”);
    se.Elements.Add(“1”);

    // Create the end of the functional group (GE segment). This must be done in a loop.
     
     
    Loop endOfFunctionalGroupLoop = functionalHeaderLoop.CreateLoop(“End Functional Group”);
    DataSegment ge = endOfFunctionalGroupLoop.CreateSegment(“GE”);
    ge.Elements.Add(“1”);
    ge.Elements.Add(“1”);

    // Finally create the end of the interchange loop (IEA segment). This must be done in a loop.
    Loop endInterchangeLoop = interchangeHeaderLoop.CreateLoop(“End Interchange”);
    DataSegment iea = endInterchangeLoop.CreateSegment(“IEA”);
    iea.Elements.Add(“1”);
    iea.Elements.Add(“1”);

    // Add the interchange loop to the EDI document
    sampleEDIFile.Loops.Add(interchangeHeaderLoop);

    // Generate the EDI file
    sampleEDIFile.GenerateEDIFile();

    Output File

    ISA*00* *00* *ZZ*InterchangeSenderID*ZZ*InterchangeReceiverID*070303*18:04*U*00401*1*1*T*:~
    GS*HC*ApplicationSenderCode*ApplicationReceiverCode*2005*132334*1*X*004010X098A1~
    ST*837*1~
    BHT*0019*00*1*20070515*094553*CH~
    SE*3*1~
    GE*1*1~
    IEA*1*1~

    in EDI Document
    Did this article answer your question?