private void btnTranslate_Click(object sender, EventArgs e) { ediDocument oEdiDoc; ediSchema oSchema; ediSchemas oSchemas; ediDataSegment oSegment; string sSegmentID = ""; string sValue = ""; string sLoopSection = ""; string sEdiFileName = ""; string sSefFileName = ""; int nArea = 0; string sBinFilename = ""; 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 oSchemas = (ediSchemas) 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 = "841_004010.X12"; sSefFileName = "841_004010.EVAL0.SEF"; // Specify SEF file to load. oSchema = (ediSchema) oEdiDoc.LoadSchema(sSefFileName, SchemaTypeIDConstants.Schema_Standard_Exchange_Format); // Specify EDI document to read. oEdiDoc.LoadEdi(sEdiFileName); // Gets the first data segment in the EDI document. oSegment = (ediDataSegment) 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; sLoopSection = 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 == "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); // Interchange Control Standards Identifier (I10) sValue = oSegment.get_DataElementValue(12, 0); // Interchange Control Version Number (I11) 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) 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 (sLoopSection == "") { 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) } // sSegmentID == "ST" } else if (sLoopSection == "SPI") { if (sSegmentID == "SPI") // Specification Identifier { sValue = oSegment.get_DataElementValue(1, 0); // Security Level Code (786) sValue = oSegment.get_DataElementValue(2, 0); // Reference Identification Qualifier (128) sValue = oSegment.get_DataElementValue(3, 0); // Reference Identification (127) sValue = oSegment.get_DataElementValue(7, 0); // Transaction Set Purpose Code (353) sValue = oSegment.get_DataElementValue(8, 0); // Report Type Code (755) sValue = oSegment.get_DataElementValue(9, 0); // Security Level Code (786) } } //sLoopSection == "SPI;SPI" } else if (nArea == 2) { // Data segments in area 2 are processed here. if (sLoopSection == "HL") { if (sSegmentID == "HL") // Hierarchical Level { sValue = oSegment.get_DataElementValue(1, 0); // Hierarchical ID Number (628) sValue = oSegment.get_DataElementValue(2, 0); // Hierarchical Parent ID Number (734) sValue = oSegment.get_DataElementValue(3, 0); // Hierarchical Level Code (735) sValue = oSegment.get_DataElementValue(4, 0); // Hierarchical Child Code (736) } // sSegmentID == "HL" } else if (sLoopSection == "HL;EFI") { if (sSegmentID == "EFI") // Electronic Format Identification { sValue = oSegment.get_DataElementValue(1, 0); // Security Level Code (786) sValue = oSegment.get_DataElementValue(4, 0); // Version Identifier (799) sValue = oSegment.get_DataElementValue(5, 0); // Program Identifier (802) sValue = oSegment.get_DataElementValue(6, 0); // Version Identifier (799) sValue = oSegment.get_DataElementValue(7, 0); // Interchange Format (801) sBinFilename = oSegment.get_DataElementValue(11, 0); // File Name (803) label1.Text = sBinFilename; sValue = oSegment.get_DataElementValue(12, 0); // Block Type (804) sValue = oSegment.get_DataElementValue(13, 0); // Record Length (787) sValue = oSegment.get_DataElementValue(14, 0); // Block Length (788) sValue = oSegment.get_DataElementValue(15, 0); // Version Identifier (799) sValue = oSegment.get_DataElementValue(16, 0); // Filter ID Code (1570) } else if (sSegmentID == "BIN") // Binary Data { sValue = oSegment.get_DataElementValue(1, 0); // Length of Binary Data (784) oSegment.get_DataElement(2).ExportValue(sPath + "copy_" + sBinFilename ); // Binary Data (785) pictureBox1.Load(sPath + "copy_" + sBinFilename); } // sSegmentID == "BIN" } //sLoopSection == "HL;EFI" } else if (nArea == 3) { // Data segments in area 3 are processed here. if (sLoopSection == "") { 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" } // sLoopSection == "" } // nArea == 3 // Get the next data segment in the document. ediDataSegment.Set(ref oSegment, oSegment.Next()); } // oSegment != null MessageBox.Show("Done"); }