This project has moved. For the latest updates, please go here.

Creating XML For EdiFact Order Message

Jun 11, 2014 at 7:36 AM
Edited Jun 11, 2014 at 8:04 AM
Firstly, thanks for a awesome piece of code.

I am busy creating a sample XML file to create a EdiFact ORDER message. But I can't seem to add my D96A Message to the Group as it seems to need a XElement. How can I convert EdiFabric.Definitions.Edifact_D96A_ORDERS.M_ORDERS to XElement?

Here is my code, the error is at
group.MESSAGES.Add(message);
        internal void CreateOrderXML()
        {
            xmlFileName = @"C:\Data\EDIFactTest.xml";

            var exportMessage = PrepareOrderItems();                       

            var serializer = new XmlSerializer(exportMessage.GetType());

            using (var fileWriter = new StreamWriter(xmlFileName))
            {
                serializer.Serialize(fileWriter, exportMessage);
            }
        }

        internal INTERCHANGE PrepareOrderItems()
        {
            var interchange = new INTERCHANGE();

            var group = new GROUP()
            {
                MESSAGES = new List<XElement> {}
            };

            #region Order Message
            var message = new M_ORDERS();

            //S_UNH
            message.S_UNH.D_0062_1 = DateTime.Now.ToString();
            message.S_UNH.C_S009.D_0065_1 = "ORDERS";
            message.S_UNH.C_S009.D_0052_2 = "D";
            message.S_UNH.C_S009.D_0054_3 = "96A";
            message.S_UNH.C_S009.D_0051_4 = "UN";
            message.S_UNH.C_S009.D_0057_5 = "EAB008";

            //S_BGM
            message.S_BGM.C_C002.D_1001_1 = "220";          //Order
            message.S_BGM.D_1004_2 = "1";                   //Order Document Number
            message.S_BGM.D_1225_3 = "9";                   //Original

            //S_DTM - Date/Time
            var s_dtm = new S_DTM()
            {
                C_C507 = new C_C507()
                {
                    D_2005_1 = "137",
                    D_2380_2 = DateTime.Now.ToShortDateString(),
                    D_2379_3 = "102"
                }
            };

            message.S_DTM.Add(s_dtm);

            //G_NAD - Buyer Address
            var g_nad = new G_NAD()
            {
                S_NAD = new S_NAD()
                {
                    D_3035_1 = "BY",
                    C_C082 = new C_C082()
                    {
                        D_3039_1 = "Company SAN",
                        D_3055_3 = "32B"
                    }
                }
            };

            message.G_NAD.Add(g_nad);

            //G_NAD - Supplier Address
            g_nad = new G_NAD()
            {
                S_NAD = new S_NAD()
                {
                    D_3035_1 = "SU",
                    C_C082 = new C_C082()
                    {
                        D_3039_1 = "Supplier SAN",
                        D_3055_3 = "31B"
                    }
                }
            };

            message.G_NAD.Add(g_nad);

            #region Order Line
            var g_lin = new G_LIN()
            {
                S_LIN = new S_LIN()
                {
                    D_1082_1 = "1",                             //Line Item Number
                    C_C212 = new C_C212()
                    {
                        D_7140_1 = "978123456789",              //ISBN13
                        D_7143_2 = "EN"                         //EAN - Reference Type
                    }
                }
            };

            var s_qty_3 = new S_QTY_3()
            {
                C_C186_3 = new C_C186_3()
                {
                    D_6063_1 = "21",                            //21 = Ordered Quantity
                    D_6060_2 = "1"                              //Quantity Ordered
                }
            };

            g_lin.S_QTY_3.Add(s_qty_3);

            var g_rff_3 = new G_RFF_3()
            {
                S_RFF_6 = new S_RFF_6()
                {
                    C_C506_6 = new C_C506_6()
                    {
                        D_1153_1 = "LI",                        //Line Item Reference Number
                        D_1154_2 = "349331"                     //Internal/External??
                    }

                }
            };

            g_lin.G_RFF_3.Add(g_rff_3);
            #endregion

            message.S_UNS = new S_UNS()
            {
                D_0081_1 = "S"
            };

            var s_cnt = new S_CNT()
            {
                C_C270 = new C_C270()
                {
                    D_6069_1 = "2",                             //Code For Total Line Items
                    D_6066_2 = "1"                              //Total Line Items
                }
            };

            message.S_CNT.Add(s_cnt);

            message.S_UNT = new S_UNT()
            {
                D_0074_1 = "11",                                //Number Of Segments in the File
                D_0062_2 = DateTime.Now.ToString()
            };

            #endregion

            #region Order Groups
            group.MESSAGES.Add(message);
            interchange.GROUPS.Add(group);
            #endregion            

            #region Message Header
            interchange.S_UNB = new S_UNB()
            {
                C_S001 = new C_S001()
                {
                    D_0001_1 = "UNOC",
                    D_0002_2 = "3"
                },

                C_S002 = new C_S002()
                {
                    D_0004_1 = "999999"
                },

                C_S003 = new C_S003()
                {
                    D_0010_1 = "1697978"
                },

                C_S004 = new C_S004()
                {
                    D_0017_1 = "091216",
                    D_0019_2 = "1258"
                },

                D_0020_5 = DateTime.Now.ToString()
            };
            #endregion

            #region Message Footer

            interchange.S_UNZ = new S_UNZ()
            {
                D_0036_1 = "1",
                D_0020_2 = DateTime.Now.ToString()
            };
            #endregion

            return interchange;
        }
My thought process is to create the INTERCHANGE and then write it to a XML file and then have EdiFabric to convert the XML file to a EdiFact message. Reason for this is that I want to keep log of the XML files generated.

Thank you.

[EDIT] Seems I just needed this line

var messageParser = new EdiParser();
group.MESSAGES.Add(messageParser.Serialize(message));

It creates the XML File now.
Coordinator
Jul 28, 2014 at 3:19 PM
Glad you found the answer yourself.