ediDocument oEdiDoc; ediSchemas oSchemas = null; ediDataSegment oSegment = null; string sSegmentID = ""; string sValue = ""; string sLoopDesc = ""; string sEdiFileName = ""; string sSefFileName = ""; int nArea = 0; 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 translate // the EDI document ediSchemas.Set(ref oSchemas, oEdiDoc.GetSchemas()); oSchemas.EnableStandardReference = false; // The FORWARD-ONLY cursor increases the performance of processing the EDI // document (see Technical Note 3 below). oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardOnly; sEdiFileName = "999_837_5010X222A1.X12"; sSefFileName = "999_005010X231A1.EVAL0.SEF"; // Specify SEF file to load. oEdiDoc.LoadSchema(sPath + sSefFileName, SchemaTypeIDConstants.Schema_Standard_Exchange_Format); // Specify EDI document to read. oEdiDoc.LoadEdi(sPath + sEdiFileName); // Gets the first data segment in the EDI document. ediDataSegment.Set(ref oSegment, oEdiDoc.FirstDataSegment); // Loop that will traverse through the EDI document from top to bottom. This // is required for FORWARD-ONLY cursor. while (oSegment != null) { // Data segments are uniquely identified by their segment Identifier (ID). // The loop section and the area (or table) properties of the data segment // are read for each iteration of the loop. sSegmentID = oSegment.ID; sLoopDesc = oSegment.LoopSection; nArea = oSegment.Area; if (nArea == 0) { // Data segments that are not designated an area (i.e. area = 0) // are control segments that are part of the Interchange or // Functional Group envelopes, and are not part of the Transaction Set. if (sSegmentID == "TA1") { sValue = oSegment.get_DataElementValue(1, 0); // Interchange Control Number listBox1.Items.Add("Received Interchange Control Number: " + sValue); sValue = oSegment.get_DataElementValue(2, 0); // Interchange Date sValue = oSegment.get_DataElementValue(3, 0); // Interchange Time sValue = oSegment.get_DataElementValue(4, 0); // Interchange Acknowledgment Code listBox1.Items.Add("Interchange Acknowledgment Code: " + sValue); sValue = oSegment.get_DataElementValue(5, 0); // Interchange Note Code } else if (sSegmentID == "ISA") { sValue = oSegment.get_DataElementValue(1, 0); // Authorization Information Qualifier (I01) sValue = oSegment.get_DataElementValue(2, 0); // Authorization Information (I02) sValue = oSegment.get_DataElementValue(3, 0); // Security Information Qualifier (I03) sValue = oSegment.get_DataElementValue(4, 0); // Security Information (I04) sValue = oSegment.get_DataElementValue(5, 0); // Interchange ID Qualifier (I05) sValue = oSegment.get_DataElementValue(6, 0); // Interchange Sender ID (I06) sValue = oSegment.get_DataElementValue(7, 0); // Interchange ID Qualifier (I05) sValue = oSegment.get_DataElementValue(8, 0); // Interchange Receiver ID (I07) sValue = oSegment.get_DataElementValue(9, 0); // Interchange Date (I08) sValue = oSegment.get_DataElementValue(10, 0); // Interchange Time (I09) sValue = oSegment.get_DataElementValue(11, 0); // Repetition Separator (I65) sValue = oSegment.get_DataElementValue(12, 0); // Interchange Control Version Number (I11) listBox1.Items.Add("999 Interchange Control Version Number: " + sValue); sValue = oSegment.get_DataElementValue(13, 0); // Interchange Control Number (I12) sValue = oSegment.get_DataElementValue(14, 0); // Acknowledgment Requested (I13) sValue = oSegment.get_DataElementValue(15, 0); // Usage Indicator (I14) sValue = oSegment.get_DataElementValue(16, 0); // Component Element Separator (I15) } else if (sSegmentID == "IEA") { sValue = oSegment.get_DataElementValue(1, 0); // Number of Included Functional Groups (I16) sValue = oSegment.get_DataElementValue(2, 0); // Interchange Control Number (I12) } else if (sSegmentID == "GS") { sValue = oSegment.get_DataElementValue(1, 0); // Functional Identifier Code (479) sValue = oSegment.get_DataElementValue(2, 0); // Application Sender's Code (142) sValue = oSegment.get_DataElementValue(3, 0); // Application Receiver's Code (124) sValue = oSegment.get_DataElementValue(4, 0); // Date (373) sValue = oSegment.get_DataElementValue(5, 0); // Time (337) sValue = oSegment.get_DataElementValue(6, 0); // Group Control Number (28) listBox1.Items.Add("999 Group Control Number: " + sValue); sValue = oSegment.get_DataElementValue(7, 0); // Responsible Agency Code (455) sValue = oSegment.get_DataElementValue(8, 0); // Version / Release / Industry Identifier Code (480) } else if (sSegmentID == "GE") { sValue = oSegment.get_DataElementValue(1, 0); // Number of Transaction Sets Included (97) sValue = oSegment.get_DataElementValue(2, 0); // Group Control Number (28) } // sSegmentID == "GE" } else if (nArea == 1) { // Data segments in area 1 are processed here. if (sLoopDesc == "") { if (sSegmentID == "ST") // Transaction Set Header { sValue = oSegment.get_DataElementValue(1, 0); // Transaction Set Identifier Code (143) sValue = oSegment.get_DataElementValue(2, 0); // Transaction Set Control Number (329) listBox1.Items.Add("999 Transaction Set Control Number: " + sValue); sValue = oSegment.get_DataElementValue(3, 0); // Implementation Convention Reference (1705) } else if (sSegmentID == "AK1") // Functional Group Response Header { sValue = oSegment.get_DataElementValue(1, 0); // Functional Identifier Code (479) sValue = oSegment.get_DataElementValue(2, 0); // Group Control Number (28) listBox1.Items.Add("Received Group Control Number: " + sValue); sValue = oSegment.get_DataElementValue(3, 0); // Version / Release / Industry Identifier Code (480) } else if (sSegmentID == "AK9") // Functional Group Response Trailer { sValue = oSegment.get_DataElementValue(1, 0); // Functional Group Acknowledge Code (715) listBox1.Items.Add("Functional Group Acknowledge Code: " + sValue); sValue = oSegment.get_DataElementValue(2, 0); // Number of Transaction Sets Included (97) sValue = oSegment.get_DataElementValue(3, 0); // Number of Received Transaction Sets (123) sValue = oSegment.get_DataElementValue(4, 0); // Number of Accepted Transaction Sets (2) sValue = oSegment.get_DataElementValue(5, 0); // Functional Group Syntax Error Code (716) sValue = oSegment.get_DataElementValue(6, 0); // Functional Group Syntax Error Code (716) sValue = oSegment.get_DataElementValue(7, 0); // Functional Group Syntax Error Code (716) sValue = oSegment.get_DataElementValue(8, 0); // Functional Group Syntax Error Code (716) sValue = oSegment.get_DataElementValue(9, 0); // Functional Group Syntax Error Code (716) } else if (sSegmentID == "SE") // Transaction Set Trailer { sValue = oSegment.get_DataElementValue(1, 0); // Number of Included Segments (96) sValue = oSegment.get_DataElementValue(2, 0); // Transaction Set Control Number (329) } // sSegmentID == "SE" } else if (sLoopDesc == "AK2") { if (sSegmentID == "AK2") // Transaction Set Response Header { sValue = oSegment.get_DataElementValue(1, 0); // Transaction Set Identifier Code (143) sValue = oSegment.get_DataElementValue(2, 0); // Transaction Set Control Number (329) listBox1.Items.Add("Received Transaction Set Control Number: " + sValue); sValue = oSegment.get_DataElementValue(3, 0); // Implementation Convention Reference (1705) } else if (sSegmentID == "IK5") // Implementation Transaction Set Response Trailer { sValue = oSegment.get_DataElementValue(1, 0); // Transaction Set Acknowledgment Code (717) listBox1.Items.Add("Transaction Set Acknowledgment Code: " + sValue); sValue = oSegment.get_DataElementValue(2, 0); // mplementation Transaction Set Syntax Error Code (618) sValue = oSegment.get_DataElementValue(3, 0); // mplementation Transaction Set Syntax Error Code (618) sValue = oSegment.get_DataElementValue(4, 0); // mplementation Transaction Set Syntax Error Code (618) sValue = oSegment.get_DataElementValue(5, 0); // mplementation Transaction Set Syntax Error Code (618) sValue = oSegment.get_DataElementValue(6, 0); // mplementation Transaction Set Syntax Error Code (618) } // sSegmentID == "IK5" } else if (sLoopDesc == "AK2;IK3") { if (sSegmentID == "IK3") // Implementation Data Segment Note { sValue = oSegment.get_DataElementValue(1, 0); // Segment ID Code (721) sValue = oSegment.get_DataElementValue(2, 0); // Segment Position in Transaction Set (719) sValue = oSegment.get_DataElementValue(3, 0); // Loop Identifier Code (447) sValue = oSegment.get_DataElementValue(4, 0); // Track (620) } else if (sSegmentID == "CTX") // Context { sValue = oSegment.get_DataElementValue(1, 1); // Context Name (9999) sValue = oSegment.get_DataElementValue(1, 2); // Context Reference (9998) sValue = oSegment.get_DataElementValue(2, 0); // Segment ID Code (721) sValue = oSegment.get_DataElementValue(3, 0); // Segment Position in Transaction Set (719) sValue = oSegment.get_DataElementValue(4, 0); // Loop Identifier Code (447) sValue = oSegment.get_DataElementValue(5, 1); // Element Position in Segment (722) sValue = oSegment.get_DataElementValue(5, 2); // Component Data Element Position in Composite (1528) sValue = oSegment.get_DataElementValue(5, 3); // Repeating Data Element Position (1686) sValue = oSegment.get_DataElementValue(6, 1); // Data Element Reference Number (725) sValue = oSegment.get_DataElementValue(6, 2); // Data Element Reference Number (725) } // sSegmentID == "CTX" } else if (sLoopDesc == "AK2;IK3;IK4") { if (sSegmentID == "IK4") // Implementation Data Element Note { sValue = oSegment.get_DataElementValue(1, 1); // Element Position in Segment (722) sValue = oSegment.get_DataElementValue(1, 2); // Component Data Element Position in Composite (1528) sValue = oSegment.get_DataElementValue(1, 3); // Repeating Data Element Position (1686) sValue = oSegment.get_DataElementValue(2, 0); // Data Element Reference Number (725) sValue = oSegment.get_DataElementValue(3, 0); // Spot (621) sValue = oSegment.get_DataElementValue(4, 0); // Copy of Bad Data Element (724) } else if (sSegmentID == "CTX") // Context { sValue = oSegment.get_DataElementValue(1, 1); // Context Name (9999) sValue = oSegment.get_DataElementValue(1, 2); // Context Reference (9998) sValue = oSegment.get_DataElementValue(2, 0); // Segment ID Code (721) sValue = oSegment.get_DataElementValue(3, 0); // Segment Position in Transaction Set (719) sValue = oSegment.get_DataElementValue(4, 0); // Loop Identifier Code (447) sValue = oSegment.get_DataElementValue(5, 1); // Element Position in Segment (722) sValue = oSegment.get_DataElementValue(5, 2); // Component Data Element Position in Composite (1528) sValue = oSegment.get_DataElementValue(5, 3); // Repeating Data Element Position (1686) sValue = oSegment.get_DataElementValue(6, 1); // Data Element Reference Number (725) sValue = oSegment.get_DataElementValue(6, 2); // Data Element Reference Number (725) } // sSegmentID == "CTX" } // sLoopDesc == "AK2;IK3;IK4" } // nArea == 1 // Get the next data segment in the document. ediDataSegment.Set(ref oSegment, oSegment.Next()); } // oSegment != null MessageBox.Show("Done"); oSchemas.Dispose(); oEdiDoc.Dispose(); }