Ejemplo n.º 1
0
        /// <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("1");

            // Ensure the object adheres to the rule
            // Skip trailer validation
            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 EdifactWriter(@"C:\Test\Output.txt", false);

                writer.Write(Helpers.CreateUnb("1"));
                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
            }
        }