private void btnGenerate_Click(object sender, EventArgs e)
        {
            ediDocument oEdiDoc;
            ediSchema oSchema;
            ediSchemas oSchemas;
            ediInterchange oInterchange = null;
            ediTransactionSet oMessage = null;
            ediDataSegment oSegment = null;
            string sEdiFileName = "";
            string sSefFileName = "";
            string sPath = AppDomain.CurrentDomain.BaseDirectory;

            // Create the top-level application object "ediDocument". 
            oEdiDoc = new ediDocument();

            // This makes certain that Framework EDI only uses the SEF file provided, 
            // and that it does not use its built-in Standard Reference table to generate 
            // the EDI document 
            oSchemas = (ediSchemas)oEdiDoc.GetSchemas();
            oSchemas.EnableStandardReference = false;

            // The FORWARD-WRITE cursor increases the performance of processing the EDI 
            // document (see Technical Note 3 below). 
            oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardWrite;

            // Terminators have to be specified. 
            oEdiDoc.SegmentTerminator = "'{13:10}";
            oEdiDoc.ElementTerminator = "+";
            oEdiDoc.CompositeTerminator = ":";
            oEdiDoc.ReleaseIndicator = "?";

            sEdiFileName = "DESTIM_D96B.EDI";
            sSefFileName = "DESTIM_D96B.EVAL60.SEF";    //evaluation SEF file

            // Specify SEF file to load. 
            oSchema = (ediSchema)oEdiDoc.LoadSchema(sPath + sSefFileName, SchemaTypeIDConstants.Schema_Standard_Exchange_Format);

            // Creates the Interchange Control Header segment (UNB). 
            ediInterchange.Set(ref oInterchange, oEdiDoc.CreateInterchange("UN", "D96B"));
            oSegment = (ediDataSegment)oInterchange.GetDataSegmentHeader();
            oSegment.set_DataElementValue(5, 0, "1");         // Interchange control reference (0020) 
            oSegment.set_DataElementValue(7, 0, "A1B2C3D4E5");         // Application reference (0026) 
            oSegment.set_DataElementValue(8, 0, "A");         // Processing priority code (0029) 
            oSegment.set_DataElementValue(9, 0, "1");         // Acknowledgement request (0031) 
            oSegment.set_DataElementValue(10, 0, "A1B2C3D4E5");         // Interchange Agreement Identifier (0032) 
            oSegment.set_DataElementValue(11, 0, "1");         // Test indicator (0035) 
            oSegment.set_DataElementValue(1, 1, "UNOA");         // Syntax identifier (0001) 
            oSegment.set_DataElementValue(1, 2, "1");         // Syntax version number (0002) 
            oSegment.set_DataElementValue(2, 1, "SENDER ID");         // Interchange sender identification (0004) 
            oSegment.set_DataElementValue(2, 2, "ZZZ");         // Partner identification code qualifier (0007) 
            oSegment.set_DataElementValue(2, 3, "A1B2C3D4E5");         // Interchange sender internal identification (0008) 
            oSegment.set_DataElementValue(3, 1, "RECEIVER ID");         // Recipient identification (0010) 
            oSegment.set_DataElementValue(3, 2, "ZZZ");         // Partner identification code qualifier (0007) 
            oSegment.set_DataElementValue(3, 3, "A1B2C3D4E5");         // Routing address (0014) 
            oSegment.set_DataElementValue(4, 1, "010101");         // Date (0017) 
            oSegment.set_DataElementValue(4, 2, "0101");         // Time (0019) 
            oSegment.set_DataElementValue(6, 1, "A1B2C3D4E5");         // Recipient reference/password (0022) 
            oSegment.set_DataElementValue(6, 2, "AA");         // Recipient's reference/password qualifier (0025) 

            
            // MESSAGE ID DESTIM - Equipment damage and repair estimate message 
            // Creates the Message header segment (UNH). 
            ediTransactionSet.Set(ref oMessage, oInterchange.CreateTransactionSet("DESTIM"));
            ediDataSegment.Set(ref oSegment, (ediDataSegment)oMessage.GetDataSegmentHeader());
            oSegment.set_DataElementValue(1, 0, "0001");         // Message reference number (0062) 
            oSegment.set_DataElementValue(2, 1, "DESTIM");         // Message type identifier (0065) 
            oSegment.set_DataElementValue(2, 2, "D");         // Message version number (0052) 
            oSegment.set_DataElementValue(2, 3, "96B");         // Message release number (0054) 
            oSegment.set_DataElementValue(2, 4, "UN");         // Controlling agency (0051) 
            oSegment.set_DataElementValue(2, 5, "");         // Association assigned code (0057) 

            // Beginning of message (BGM) 
            ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment("BGM"));
            oSegment.set_DataElementValue(1, 1, "29");         // Document/message name, coded (1001) 
            oSegment.set_DataElementValue(2, 1, "8888");         // Document/message number (1004) 
            oSegment.set_DataElementValue(3, 0, "9");         // Message function, coded (1225) 
            oSegment.set_DataElementValue(4, 0, "AB");         // Response type, coded (4343) 

            // Date/time/period (DTM) 
            ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment("DTM"));
            oSegment.set_DataElementValue(1, 1, "137");         // Date/time/period qualifier (2005) 
            oSegment.set_DataElementValue(1, 2, "201402211545");         // Date/time/period (2380) 
            oSegment.set_DataElementValue(1, 3, "203");         // Date/time/period format qualifier (2379) 

            // Currencies (CUX) 
            ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment("CUX"));
            oSegment.set_DataElementValue(1, 1, "5");         // Currency details qualifier (6347) 
            oSegment.set_DataElementValue(1, 2, "USD");         // Currency, coded (6345) 

            // Reference (RFF) 
            ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment("RFF"));
            oSegment.set_DataElementValue(1, 1, "CN");         // Reference qualifier (1153) 
            oSegment.set_DataElementValue(1, 2, "SFO1RSFO1R960818E");         // Reference number (1154) 

            // Reference (RFF) 
            ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment("RFF(2)"));
            oSegment.set_DataElementValue(1, 1, "CT");         // Reference qualifier (1153) 
            oSegment.set_DataElementValue(1, 2, "S991234");         // Reference number (1154) 

            // Free text (FTX) 
            ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment("FTX"));
            oSegment.set_DataElementValue(1, 0, "AAI");         // Text subject qualifier (4451) 
            oSegment.set_DataElementValue(4, 1, "PLEASE RESPOND BY FRIDAY");         // Free text (4440) 

            // Equipment details (EQD) 
            ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"EQD\EQD"));
            oSegment.set_DataElementValue(1, 0, "CN");         // Equipment qualifier (8053) 
            oSegment.set_DataElementValue(2, 1, "MLCU9106548");         // Equipment identification number (8260) 
            oSegment.set_DataElementValue(3, 1, "");         // Equipment size and type identification (8155) 
            oSegment.set_DataElementValue(3, 2, "102");         // Code list qualifier (1131) 
            oSegment.set_DataElementValue(3, 3, "5");         // Code list responsible agency, coded (3055) 
            oSegment.set_DataElementValue(3, 4, "4320");         // Equipment size and type (8154) 
            oSegment.set_DataElementValue(6, 0, "");         // Full/empty indicator, coded (8169) 

            if (1 == 1) //NAD loop
            {
                // Name and address (NAD) 
                ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"NAD\NAD"));
                oSegment.set_DataElementValue(1, 0, "MS");         // Party qualifier (3035) 
                oSegment.set_DataElementValue(2, 1, "USRCHCCIA");         // Party identification (3039) 
                oSegment.set_DataElementValue(2, 2, "160");         // Code list qualifier (1131) 
                oSegment.set_DataElementValue(2, 3, "20");         // Code list responsible agency, coded (3055) 

                // Monetary amount (MOA) 
                ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"NAD\MOA"));
                oSegment.set_DataElementValue(1, 1, "259");         // Monetary amount type qualifier (5025) 
                oSegment.set_DataElementValue(1, 2, "100.00");         // Monetary amount (5004) 
                oSegment.set_DataElementValue(1, 3, "USD");         // Currency, coded (6345) 

                // Contact information (CTA) 
                ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"NAD\CTA\CTA"));
                oSegment.set_DataElementValue(1, 0, "IC");         // Contact function, coded (3139) 
                oSegment.set_DataElementValue(2, 1, "");         // Department or employee identification (3413) 
                oSegment.set_DataElementValue(2, 2, "JOE");         // Department or employee (3412) 
            }
            

            // Section control (UNS) 
            ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment("UNS"));
            oSegment.set_DataElementValue(1, 0, "D");         // Section identification (0081) 

            if (1 == 0) // RFF loop
            {
                // Reference (RFF) 
                ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"RFF\RFF"));
                oSegment.set_DataElementValue(1, 1, "32");         // Reference qualifier (1153) 
                oSegment.set_DataElementValue(1, 2, "");         // Reference number (1154) 

                // Date/time/period (DTM) 
                ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"RFF\DTM"));
                oSegment.set_DataElementValue(1, 1, "400");         // Date/time/period qualifier (2005) 
                oSegment.set_DataElementValue(1, 2, "");         // Date/time/period (2380) 
                oSegment.set_DataElementValue(1, 3, "");         // Date/time/period format qualifier (2379) 

                // Name and address (NAD) 
                ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"RFF\NAD"));
                oSegment.set_DataElementValue(1, 0, "");         // Party qualifier (3035) 
                oSegment.set_DataElementValue(2, 1, "");         // Party identification (3039) 
                oSegment.set_DataElementValue(2, 2, "");         // Code list qualifier (1131) 
                oSegment.set_DataElementValue(2, 3, "");         // Code list responsible agency, coded (3055) 

                // Monetary amount (MOA) 
                ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"RFF\MOA"));
                oSegment.set_DataElementValue(1, 1, "7");         // Monetary amount type qualifier (5025) 
                oSegment.set_DataElementValue(1, 2, "");         // Monetary amount (5004) 
                oSegment.set_DataElementValue(1, 3, "");         // Currency, coded (6345) 
            }

            if (1 == 1) // LIN loop
            {
                // Line item (LIN) 
                ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"LIN\LIN"));
                oSegment.set_DataElementValue(1, 0, "1");         // Line item number (1082) 
                oSegment.set_DataElementValue(2, 0, "");         // Action request/notification, coded (1229) 

                // Dimensions (DIM) 
                ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"LIN\DIM"));
                oSegment.set_DataElementValue(1, 0, "");         // Dimension qualifier (6145)
                oSegment.set_DataElementValue(2, 1, "");         // Measure unit qualifier (6411) 
                oSegment.set_DataElementValue(2, 2, "");         // Length dimension (6168) 
                oSegment.set_DataElementValue(2, 3, "");         // Width dimension (6140) 
                oSegment.set_DataElementValue(2, 4, "");         // Height dimension (6008) 

                // Quantity (QTY) 
                ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"LIN\QTY"));
                oSegment.set_DataElementValue(1, 1, "207");         // Quantity qualifier (6063) 
                oSegment.set_DataElementValue(1, 2, "1.5");         // Quantity (6060) 

                // Free text (FTX) 
                ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"LIN\FTX"));
                oSegment.set_DataElementValue(1, 0, "");         // Text subject qualifier (4451) 
                oSegment.set_DataElementValue(3, 1, "");         // Free text identification (4441) 
                oSegment.set_DataElementValue(3, 3, "");         // Code list responsible agency, coded (3055) 
                oSegment.set_DataElementValue(4, 1, "");         // Free text (4440) 
                oSegment.set_DataElementValue(4, 2, "");         // Free text (4440) 
                oSegment.set_DataElementValue(4, 3, "");         // Free text (4440) 
                oSegment.set_DataElementValue(4, 4, "");         // Free text (4440) 
                oSegment.set_DataElementValue(4, 5, "");         // Free text (4440) 

                if (1 == 1) // LIN\DAM loop
                {
                    // Damage (DAM) 
                    ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"LIN\DAM\DAM"));
                    oSegment.set_DataElementValue(1, 0, "1");         // Damage details qualifier (7493) 
                    oSegment.set_DataElementValue(2, 1, "EZXN");         // Type of damage, coded (7501) 
                    oSegment.set_DataElementValue(2, 2, "ZZZ");         // Code list qualifier (1131) 
                    oSegment.set_DataElementValue(2, 3, "5");         // Code list responsible agency, coded (3055) 
                    oSegment.set_DataElementValue(2, 4, "EZXN");         // Type of damage (7500) 
                    oSegment.set_DataElementValue(3, 1, "GS");         // Damage area identification (7503) 
                    oSegment.set_DataElementValue(3, 2, "ZZZ");         // Code list qualifier (1131) 
                    oSegment.set_DataElementValue(3, 3, "5");         // Code list responsible agency, coded (3055) 
                    oSegment.set_DataElementValue(3, 4, "");         // Damage area (7502) 
                    oSegment.set_DataElementValue(4, 1, "");         // Damage severity, coded (7509) 
                    oSegment.set_DataElementValue(4, 2, "");         // Code list qualifier (1131) 
                    oSegment.set_DataElementValue(4, 3, "");         // Code list responsible agency, coded (3055) 
                    oSegment.set_DataElementValue(4, 4, "");         // Damage severity (7508) 
                    oSegment.set_DataElementValue(5, 1, "");         // Action request/notification, coded (1229) 
                    oSegment.set_DataElementValue(5, 2, "");         // Code list qualifier (1131) 
                    oSegment.set_DataElementValue(5, 3, "");         // Code list responsible agency, coded (3055) 
                    oSegment.set_DataElementValue(5, 4, "");         // Action request/notification (1228) 

                    // Component details (COD) 
                    ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"LIN\DAM\COD"));
                    oSegment.set_DataElementValue(1, 1, "PSC");         // Type of unit/component, coded (7505) 
                    oSegment.set_DataElementValue(1, 2, "ZZZ");         // Code list qualifier (1131) 
                    oSegment.set_DataElementValue(1, 3, "5");         // Code list responsible agency, coded (3055) 
                    oSegment.set_DataElementValue(1, 4, "");         // Type of unit/component (7504) 
                    oSegment.set_DataElementValue(2, 1, "SK");         // Component material, coded (7507) 
                    oSegment.set_DataElementValue(2, 2, "ZZZ");         // Code list qualifier (1131) 
                    oSegment.set_DataElementValue(2, 3, "5");         // Code list responsible agency, coded (3055) 
                    oSegment.set_DataElementValue(2, 4, "");         // Component material (7506) 
                }

                if (1 == 1) // LIN\RTE loop
                {
                    // Rate details (RTE) 
                    ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"LIN\RTE\RTE"));
                    oSegment.set_DataElementValue(1, 1, "2");         // Rate type qualifier (5419) 
                    oSegment.set_DataElementValue(1, 2, "39.00");         // Rate per unit (5420) 

                    // Quantity (QTY) 
                    ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"LIN\RTE\QTY"));
                    oSegment.set_DataElementValue(1, 1, "1");         // Quantity qualifier (6063) 
                    oSegment.set_DataElementValue(1, 2, "4");         // Quantity (6060) 
                    oSegment.set_DataElementValue(1, 3, "HUR");         // Measure unit qualifier (6411) 
                }

                if (1 == 1) // LIN\NAD loop
                {
                    // Name and address (NAD) 
                    ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"LIN\NAD\NAD"));
                    oSegment.set_DataElementValue(1, 0, "CF");         // Party qualifier (3035) 
                    oSegment.set_DataElementValue(2, 1, "MEAU");         // Party identification (3039) 
                    oSegment.set_DataElementValue(2, 2, "172");         // Code list qualifier (1131) 
                    oSegment.set_DataElementValue(2, 3, "16");         // Code list responsible agency, coded (3055)

                    if (1 == 1) // LIN\NAD\MOA loop
                    {
                        // Monetary amount (MOA) 
                        ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"LIN\NAD\MOA\MOA"));
                        oSegment.set_DataElementValue(1, 1, "185");         // Monetary amount type qualifier (5025) 
                        oSegment.set_DataElementValue(1, 2, "9.75");         // Monetary amount (5004) 
                        oSegment.set_DataElementValue(1, 3, "USD");         // Currency, coded (6345) 

                        // Duty/tax/fee details (TAX) 
                        ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment(@"LIN\NAD\MOA\TAX"));
                        oSegment.set_DataElementValue(1, 0, "7");         // Duty/tax/fee function qualifier (5283) 
                        oSegment.set_DataElementValue(2, 1, "LOC");         // Duty/tax/fee type, coded (5153) 
                        oSegment.set_DataElementValue(4, 0, "");         // Duty/tax/fee assessment basis (5286) 
                        oSegment.set_DataElementValue(5, 1, "");         // Duty/tax/fee rate identification (5279) 
                        oSegment.set_DataElementValue(5, 2, "");         // Code list qualifier (1131) 
                        oSegment.set_DataElementValue(5, 3, "");         // Code list responsible agency, coded (3055) 
                        oSegment.set_DataElementValue(5, 4, "8.5");         // Duty/tax/fee rate (5278) 

                    }// LIN\NAD\MOA loop

                }// LIN\NAD loop

            }// LIN loop
            

            // Control total (CNT) 
            ediDataSegment.Set(ref oSegment, oMessage.CreateDataSegment("CNT"));
            oSegment.set_DataElementValue(1, 1, "12");         // Control qualifier (6069) 
            oSegment.set_DataElementValue(1, 2, "217.30");         // Control value (6066) 

            MessageBox.Show(oEdiDoc.GetEdiString());

            oEdiDoc.Save(sEdiFileName, 0);

            MessageBox.Show("Done");

        }
    

    Click here to download a trial version of the Framework EDI