private void button1_Click(object sender, System.EventArgs e) { //This is just an example program to demonstrate how th translate a UN/EDIFACT INVOIC file in C# //using the Framework EDI .NET hybrid component ediDocument oEdiDoc = null; ediSchema oSchema = null; ediSchemas oSchemas = null; ediDataSegment oSegment = null; string sSegmentID = ""; string sLoopSection = ""; int nArea = 0; string sValue = ""; string sPath = AppDomain.CurrentDomain.BaseDirectory; string sSefFile = "INVOIC_S93A.SEF"; string sEdifile = "INVOIC.TXT"; string sNadQlfr = ""; Cursor = Cursors.WaitCursor; //CREATES EDIDOC OBJECT ediDocument.Set(ref oEdiDoc, new ediDocument()); //THIS MAKES CERTAIN THAT FREDI ONLY USES THE SEF FILE PROVIDED, AND THAT IT DOES //NOT USE ITS BUILT-IN STANDARD REFERENCE TABLE TO GENERATE THE EDI FILE. ediSchemas.Set(ref oSchemas,(ediSchemas) oEdiDoc.GetSchemas()); oSchemas.EnableStandardReference = false; //THIS OPTIONS STOPS FREDI FROM KEEPING ALL THE SEGMENTS IN MEMORY oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardOnly; //This terminators are used if the EDI file does not have a UNA segment //GET TERMINATORS oEdiDoc.SegmentTerminator = "'"; oEdiDoc.ElementTerminator = "+"; oEdiDoc.CompositeTerminator = ":"; oEdiDoc.ReleaseIndicator = "?"; //LOADS THE SEF FILE ediSchema.Set(ref oSchema, (ediSchema) oEdiDoc.LoadSchema(sSefFile, 0)); //LOADS THE EDI FILE oEdiDoc.LoadEdi(sEdifile); //GETS THE FIRST DATA SEGMENT ediDataSegment.Set(ref oSegment, (ediDataSegment) oEdiDoc.FirstDataSegment); //LOOP THAT WILL TRAVERSE THRU EDI FILE FROM TOP TO BOTTOM while ( oSegment != null ) { //DATA SEGMENTS ARE IDENTIFIED BY THEIR ID, THE LOOP SECTION AND AREA //(OR TABLE) NUMBER THAT THEY ARE IN. sSegmentID = oSegment.ID; sLoopSection = oSegment.LoopSection; nArea = oSegment.Area; if (nArea == 0) { if (sLoopSection == "") { if (sSegmentID == "UNB") { sValue = oSegment.get_DataElementValue(1,1); //Syntax identifier sValue = oSegment.get_DataElementValue(1,2); //Syntax version number sValue = oSegment.get_DataElementValue(1,3); //Service code list directory version number sValue = oSegment.get_DataElementValue(1,4); //Character encoding, coded sValue = oSegment.get_DataElementValue(2,1); //Interchange sender identification sValue = oSegment.get_DataElementValue(2,2); //Partner identification code qualifier sValue = oSegment.get_DataElementValue(2,3); //Interchange sender internal identification sValue = oSegment.get_DataElementValue(2,4); //Interchange sender internal sub-identification sValue = oSegment.get_DataElementValue(3,1); //Recipient identification sValue = oSegment.get_DataElementValue(3,2); //Partner identification code qualifier sValue = oSegment.get_DataElementValue(3,3); //Routing address sValue = oSegment.get_DataElementValue(3,4); //Interchange Recipient Internal Sub-Identification sValue = oSegment.get_DataElementValue(4,1); //Date sValue = oSegment.get_DataElementValue(4,2); //Time listBox1.Items.Add(oSegment.get_DataElement(5).Description + " = " + oSegment.get_DataElementValue(5,0)); //Interchange control reference sValue = oSegment.get_DataElementValue(6,1); //Recipient reference/password sValue = oSegment.get_DataElementValue(6,2); //Recipient's reference/password qualifier sValue = oSegment.get_DataElementValue(7,0); //Application reference sValue = oSegment.get_DataElementValue(8,0); //Processing priority code sValue = oSegment.get_DataElementValue(9,0); //Acknowledgement request sValue = oSegment.get_DataElementValue(10,0); //Interchange Agreement Identifier sValue = oSegment.get_DataElementValue(11,0); //Test indicator } //sSegmentID } //sLoopSection } else if (nArea == 1) { if (sLoopSection == "") { if (sSegmentID == "UNH") { listBox1.Items.Add(oSegment.get_DataElement(1).Description + " = " + oSegment.get_DataElementValue(1,0)); //Message reference number sValue = oSegment.get_DataElementValue(2,1); //Message type identifier sValue = oSegment.get_DataElementValue(2,2); //Message version number sValue = oSegment.get_DataElementValue(2,3); //Message release number sValue = oSegment.get_DataElementValue(2,4); //Controlling agency sValue = oSegment.get_DataElementValue(2,5); //Association assigned code } else if (sSegmentID == "BGM") { listBox1.Items.Add(oSegment.get_DataElement(1).Description + " = " + oSegment.get_DataElementValue(1,1)); //Document/message name, coded sValue = oSegment.get_DataElementValue(1,2); //Code list qualifier sValue = oSegment.get_DataElementValue(1,3); //Code list responsible agency, coded sValue = oSegment.get_DataElementValue(1,4); //Document/message name sValue = oSegment.get_DataElementValue(2,0); //Document/message name sValue = oSegment.get_DataElementValue(3,0); //Document/message name } else if (sSegmentID == "DTM") { sValue = oSegment.get_DataElementValue(1,1); //Date/time/period qualifier listBox1.Items.Add(oSegment.get_DataElement(1).Description + " = " + oSegment.get_DataElementValue(1,2)); //Date/time/period sValue = oSegment.get_DataElementValue(1,3); //Date/time/period format qualifier } //SegmentID } else if (sLoopSection == "RFF") { if (sSegmentID == "RFF") { sValue = oSegment.get_DataElementValue(1,1); //Reference qualifier listBox1.Items.Add(oSegment.get_DataElement(1).Description + " = " + oSegment.get_DataElementValue(1,2)); //Reference number sValue = oSegment.get_DataElementValue(1,3); //Line number } //SegmentID } else if (sLoopSection == "NAD") { if (sSegmentID == "NAD") { sNadQlfr = oSegment.get_DataElementValue(1,0); //Party qualifier } if (sNadQlfr == "RE") //receiver address information { if (sSegmentID == "NAD") { sValue = oSegment.get_DataElementValue(1,0); //Party qualifier sValue = oSegment.get_DataElementValue(2,1); //Party identification sValue = oSegment.get_DataElementValue(2,2); //Code list qualifier sValue = oSegment.get_DataElementValue(2,3); //Code list responsible agency, coded sValue = oSegment.get_DataElementValue(3,1); //Name and address line sValue = oSegment.get_DataElementValue(3,2); //Name and address line sValue = oSegment.get_DataElementValue(3,3); //Name and address line sValue = oSegment.get_DataElementValue(3,4); //Name and address line sValue = oSegment.get_DataElementValue(3,5); //Name and address line listBox1.Items.Add(oSegment.get_DataElement(4).Description + " receiver = " + oSegment.get_DataElementValue(4,1)); //Party name sValue = oSegment.get_DataElementValue(4,2); //Party name sValue = oSegment.get_DataElementValue(4,3); //Party name } //SegmentID } else if (sNadQlfr == "ST") //ship-to address information { if (sSegmentID == "NAD") { sValue = oSegment.get_DataElementValue(1,0); //Party qualifier sValue = oSegment.get_DataElementValue(2,1); //Party identification sValue = oSegment.get_DataElementValue(2,2); //Code list qualifier sValue = oSegment.get_DataElementValue(2,3); //Code list responsible agency, coded sValue = oSegment.get_DataElementValue(3,1); //Name and address line sValue = oSegment.get_DataElementValue(3,2); //Name and address line sValue = oSegment.get_DataElementValue(3,3); //Name and address line sValue = oSegment.get_DataElementValue(3,4); //Name and address line sValue = oSegment.get_DataElementValue(3,5); //Name and address line listBox1.Items.Add(oSegment.get_DataElement(4).Description + " ship-to = " + oSegment.get_DataElementValue(4,1)); //Party name sValue = oSegment.get_DataElementValue(4,2); //Party name sValue = oSegment.get_DataElementValue(4,3); //Party name } //SegmentID } else if (sNadQlfr == "BY") //buyer address information { if (sSegmentID == "NAD") { sValue = oSegment.get_DataElementValue(1,0); //Party qualifier sValue = oSegment.get_DataElementValue(2,1); //Party identification sValue = oSegment.get_DataElementValue(2,2); //Code list qualifier sValue = oSegment.get_DataElementValue(2,3); //Code list responsible agency, coded sValue = oSegment.get_DataElementValue(3,1); //Name and address line sValue = oSegment.get_DataElementValue(3,2); //Name and address line sValue = oSegment.get_DataElementValue(3,3); //Name and address line sValue = oSegment.get_DataElementValue(3,4); //Name and address line sValue = oSegment.get_DataElementValue(3,5); //Name and address line listBox1.Items.Add(oSegment.get_DataElement(4).Description + " buyer = " + oSegment.get_DataElementValue(4,1)); //Party name sValue = oSegment.get_DataElementValue(4,2); //Party name sValue = oSegment.get_DataElementValue(4,3); //Party name } //SegmentID } } else if (sLoopSection == "NAD;RFF") { if (sNadQlfr == "RE") //receiver reference information { if (sSegmentID == "RFF") { sValue = oSegment.get_DataElementValue(1,1); //Reference qualifier listBox1.Items.Add(oSegment.get_DataElement(1).Description + " receiver = " + oSegment.get_DataElementValue(1,2)); //Reference number sValue = oSegment.get_DataElementValue(1,3); //Line number } //SegmentID } else if (sNadQlfr == "ST") //ship-to reference information { if (sSegmentID == "RFF") { sValue = oSegment.get_DataElementValue(1,1); //Reference qualifier listBox1.Items.Add(oSegment.get_DataElement(1).Description + " ship-to = " + oSegment.get_DataElementValue(1,2)); //Reference number sValue = oSegment.get_DataElementValue(1,3); //Line number } //SegmentID } else if (sNadQlfr == "BY") //buyer reference information { if (sSegmentID == "RFF") { sValue = oSegment.get_DataElementValue(1,1); //Reference qualifier listBox1.Items.Add(oSegment.get_DataElement(1).Description + " buyer = " + oSegment.get_DataElementValue(1,2)); //Reference number sValue = oSegment.get_DataElementValue(1,3); //Line number } //SegmentID } } else if (sLoopSection == "NAD;CTA") { if (sNadQlfr == "RE") //receiver contact information { if (sSegmentID == "CTA") { sValue = oSegment.get_DataElementValue(1,0); //Contact function, coded sValue = oSegment.get_DataElementValue(2,1); //Department or employee identification listBox1.Items.Add(oSegment.get_DataElement(2).Description + " receiver = " + oSegment.get_DataElementValue(2,2)); //Department or employee } else if (sSegmentID == "COM") { sValue = oSegment.get_DataElementValue(1,1); //Communication number sValue = oSegment.get_DataElementValue(1,2); //Communication channel qualifier } //SegmentID } else if (sNadQlfr == "ST") //ship-to contact information { if (sSegmentID == "CTA") { sValue = oSegment.get_DataElementValue(1,0); //Contact function, coded sValue = oSegment.get_DataElementValue(2,1); //Department or employee identification listBox1.Items.Add(oSegment.get_DataElement(2).Description + " ship-to = " + oSegment.get_DataElementValue(2,2)); //Department or employee } else if (sSegmentID == "COM") { sValue = oSegment.get_DataElementValue(1,1); //Communication number sValue = oSegment.get_DataElementValue(1,2); //Communication channel qualifier } //SegmentID } else if (sNadQlfr == "BY") //buyer contact information { if (sSegmentID == "CTA") { sValue = oSegment.get_DataElementValue(1,0); //Contact function, coded sValue = oSegment.get_DataElementValue(2,1); //Department or employee identification listBox1.Items.Add(oSegment.get_DataElement(2).Description + " buyer = " + oSegment.get_DataElementValue(2,2)); //Department or employee } else if (sSegmentID == "COM") { sValue = oSegment.get_DataElementValue(1,1); //Communication number sValue = oSegment.get_DataElementValue(1,2); //Communication channel qualifier } //SegmentID } } else if (sLoopSection == "CUX") { if (sSegmentID == "CUX") { sValue = oSegment.get_DataElementValue(1,1); //Currency details qualifier sValue = oSegment.get_DataElementValue(1,2); //Currency, coded sValue = oSegment.get_DataElementValue(1,3); //Currency qualifier sValue = oSegment.get_DataElementValue(1,4); //Currency rate base } //SegmentID } else if (sLoopSection == "ALC") { if (sSegmentID == "ALC") { sValue = oSegment.get_DataElementValue(1,0); //Allowance or charge qualifier sValue = oSegment.get_DataElementValue(2,1); //Allowance or charge number sValue = oSegment.get_DataElementValue(2,2); //Charge/allowance description, coded sValue = oSegment.get_DataElementValue(3,0); //Settlement, coded sValue = oSegment.get_DataElementValue(4,0); //Calculation sequence indicator, coded sValue = oSegment.get_DataElementValue(5,1); //Special services, coded sValue = oSegment.get_DataElementValue(5,2); //Code list qualifier sValue = oSegment.get_DataElementValue(5,3); //Code list responsible agency, coded } //SegmentID } else if (sLoopSection == "ALC;PCD") { if (sSegmentID == "PCD") { sValue = oSegment.get_DataElementValue(1,1); //Percentage qualifier sValue = oSegment.get_DataElementValue(1,2); //Percentage sValue = oSegment.get_DataElementValue(1,3); //Percentage basis, coded sValue = oSegment.get_DataElementValue(1,4); //Code list qualifier sValue = oSegment.get_DataElementValue(1,5); //Code list responsible agency, coded } //SegmentID } else if (sLoopSection == "ALC;MOA") { if (sSegmentID == "MOA") { sValue = oSegment.get_DataElementValue(1,1); //Monetary amount type qualifier listBox1.Items.Add(oSegment.get_DataElement(1).Description + " allowance = " + oSegment.get_DataElementValue(1,2)); //Monetary amount sValue = oSegment.get_DataElementValue(1,3); //Currency, coded sValue = oSegment.get_DataElementValue(1,4); //Currency qualifier sValue = oSegment.get_DataElementValue(1,5); //Status, coded } //sSegmentID } //sLoopSection } else if (nArea == 2) { if (sLoopSection == "LIN") { if (sSegmentID == "LIN") { listBox1.Items.Add(oSegment.get_DataElement(1).Description + " = " + oSegment.get_DataElementValue(1,0)); //Line item number sValue = oSegment.get_DataElementValue(2,0); //Action request/notification, coded listBox1.Items.Add(oSegment.get_DataElement(3).Description + " = " + oSegment.get_DataElementValue(3,1)); //Item number sValue = oSegment.get_DataElementValue(3,2); //Item number type, coded sValue = oSegment.get_DataElementValue(3,3); //Code list qualifier sValue = oSegment.get_DataElementValue(3,4); //Code list responsible agency, coded } else if (sSegmentID == "QTY") { sValue = oSegment.get_DataElementValue(1,1); //Quantity qualifier sValue = oSegment.get_DataElementValue(1,2); //Quantity sValue = oSegment.get_DataElementValue(1,3); //Measure unit qualifier } //SegmentID } else if (sLoopSection == "LIN;PRI") { if (sSegmentID == "PRI") { sValue = oSegment.get_DataElementValue(1,1); //Price qualifier listBox1.Items.Add(oSegment.get_DataElement(1).Description + " = " + oSegment.get_DataElementValue(1,2)); //Price sValue = oSegment.get_DataElementValue(1,3); //Price type, coded sValue = oSegment.get_DataElementValue(1,4); //Price type qualifier sValue = oSegment.get_DataElementValue(1,5); //Unit price basis sValue = oSegment.get_DataElementValue(1,6); //Measure unit qualifier } //sSegmentID } //sLoopSection } else if (nArea == 3) { if (sLoopSection == "") { if (sSegmentID == "UNS") { sValue = oSegment.get_DataElementValue(1,0); //Section identification } //SegmentID } else if (sLoopSection == "MOA") { if (sSegmentID == "MOA") { sValue = oSegment.get_DataElementValue(1,1); //Monetary amount type qualifier sValue = oSegment.get_DataElementValue(1,2); //Monetary amount sValue = oSegment.get_DataElementValue(1,3); //Currency, coded sValue = oSegment.get_DataElementValue(1,4); //Currency qualifier sValue = oSegment.get_DataElementValue(1,5); //Status, coded } //SegmentID } else if (sLoopSection == "TAX") { if (sSegmentID == "TAX") { sValue = oSegment.get_DataElementValue(1,0); //Duty/tax/fee function qualifier sValue = oSegment.get_DataElementValue(2,1); //Duty/tax/fee type, coded sValue = oSegment.get_DataElementValue(2,2); //Code list qualifier sValue = oSegment.get_DataElementValue(2,3); //Code list responsible agency, coded sValue = oSegment.get_DataElementValue(2,4); //Duty/tax/fee type sValue = oSegment.get_DataElementValue(3,1); //Duty/tax/fee account identification sValue = oSegment.get_DataElementValue(3,2); //Code list qualifier sValue = oSegment.get_DataElementValue(3,3); //Code list responsible agency, coded sValue = oSegment.get_DataElementValue(4,0); //Duty/tax/fee assessment basis sValue = oSegment.get_DataElementValue(5,1); //Duty/tax/fee rate identification sValue = oSegment.get_DataElementValue(5,2); //Code list qualifier sValue = oSegment.get_DataElementValue(5,3); //Code list responsible agency, coded sValue = oSegment.get_DataElementValue(5,4); //Duty/tax/fee rate sValue = oSegment.get_DataElementValue(5,5); //Duty/tax/fee rate basis identification sValue = oSegment.get_DataElementValue(5,6); //Code list qualifier sValue = oSegment.get_DataElementValue(5,7); //Code list responsible agency, coded sValue = oSegment.get_DataElementValue(6,0); //Duty/tax/fee category, coded } else if (sSegmentID == "MOA") { sValue = oSegment.get_DataElementValue(1,1); //Monetary amount type qualifier listBox1.Items.Add(oSegment.get_DataElement(1).Description + " invoice = " + oSegment.get_DataElementValue(1,2)); //Monetary amount sValue = oSegment.get_DataElementValue(1,3); //Currency, coded sValue = oSegment.get_DataElementValue(1,4); //Currency qualifier sValue = oSegment.get_DataElementValue(1,5); //Status, coded } //sSegmentID } //sLoopSection } //nArea //GETS THE NEXT DATA SEGMENT ediDataSegment.Set(ref oSegment, (ediDataSegment) oSegment.Next()); //oSegment = (ediDataSegment) oSegment.Next(); } //while Cursor = Cursors.Default; MessageBox.Show("Done"); } }