/// <summary> /// Generate and write EDI document to a file /// </summary> static void WriteSingleInvoiceToFile() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); // 1. Construct the invoice message with data from database, service or domain objects\logic. var invoice = CreateInvoice("00000001"); const string folder = @"C:\test"; if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } // 2. Validate it to ensure the object adheres to the rule // Always skip trailer validation because all trailers are automatically generated by the writer MessageErrorContext errorContext; if (invoice.IsValid(out errorContext, true)) { Debug.WriteLine("Message {0} with control number {1} is valid.", errorContext.Name, errorContext.ControlNumber); // 3. Use default encoding and no segment postfix // Write directly to a file var writer = new X12Writer(string.Format("{0}\\output.txt", folder), false); writer.Write(Helpers.CreateIsa("000011111")); writer.Write(Helpers.CreateGs("111111111")); writer.Write(invoice); // 4. Always flush at the end to release the cache writer.Flush(); writer.Dispose(); Debug.WriteLine("Written to file."); } else { // The purchase order is invalid } }