Example #1
0
        /// <summary>
        /// Write Purchase Order
        /// </summary>
        static void Write()
        {
            using (var stream = new MemoryStream())
            {
                var transaction = X12TransactionBuilders.BuildPurchaseOrder("1");

                MessageErrorContext mec;
                if (transaction.IsValid(out mec, new ValidationSettings {
                    SkipTrailerValidation = true
                }))
                {
                    //  valid
                    using (var writer = new X12Writer(stream))
                    {
                        writer.Write(SegmentBuilders.BuildIsa("1"));
                        writer.Write(SegmentBuilders.BuildGs("1"));
                        writer.Write(transaction);
                    }

                    var ediString = stream.LoadToString();
                }
                else
                {
                    //  invalid
                    var errors = mec.Flatten();
                }
            }
        }
Example #2
0
        /// <summary>
        /// Generate and write EDI document to a stream
        /// </summary>
        public static void WriteSinglePurchaseOrderToStream()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

            //  1.  Construct the purchase order
            var po = X12TransactionBuilders.BuildPurchaseOrder("1");

            //  2.  Validate it by skipping trailer validation
            MessageErrorContext errorContext;

            if (po.IsValid(out errorContext, new ValidationSettings {
                SkipTrailerValidation = true
            }))
            {
                Debug.WriteLine("Message {0} with control number {1} is valid.", errorContext.Name, errorContext.ControlNumber);

                using (var stream = new MemoryStream())
                {
                    using (var writer = new X12Writer(stream))
                    {
                        //  3.  Begin with ISA segment
                        writer.Write(SegmentBuilders.BuildIsa("1"));
                        //  4.  Follow up with GS segment
                        writer.Write(SegmentBuilders.BuildGs("1"));
                        //  5.  Then write the purchase order(s)
                        writer.Write(po);
                    }

                    Debug.Write(stream.LoadToString());
                }
            }
            else
            {
                //  The purchase order is invalid
                Debug.WriteLine("Message {0} with control number {1} is invalid with errors:", errorContext.Name,
                                errorContext.ControlNumber);

                //  List all error messages
                var errors = errorContext.Flatten();
                foreach (var error in errors)
                {
                    Debug.WriteLine(error);
                }
            }
        }