How to pack multiple object arrays into Xml (C#, bpel)

Nothing happened these past two days. I took a look at the code of my senior and found that he wrote a more interesting code. I will analyze and record it here.

1. Situation analysis

There are five pieces of data in the picture above. To transmit these five pieces of data to the backend, the number of pieces, operators, events, etc. must also be transmitted to the backend. I looked at other colleagues’ codes before and found that some of them performed two transmissions, which was time-consuming and labor-intensive. This is definitely not the optimal solution.

2. Explanation of senior code

MesList<YARD_DEVICE> colDevice = new MesList<YARD_DEVICE>() { XmlItemName = "ORIGINALDEVICELIST" };
                for (int i = 0; i < grdStockIn.Rows.Count; i + + )
                {
                    if (grdStockIn.Rows[i].Cells["CHECKBOX"].Text == "True")
                    {
                        colDevice.Add(new YARD_DEVICE()
                        {
                            SITENAME = ConnectionInfo.SiteName,
                            RECEIVEREQUESTNAME = grdStockIn.Rows[i].Cells["RECEIVEREQUESTNAME"].Text,
                            PONO = grdStockIn.Rows[i].Cells["PONO"].Text,
                            CONTAINER = grdStockIn.Rows[i].Cells["CONTAINER"].Text,
                            DEVICE = grdStockIn.Rows[i].Cells["DEVICE"].Text,
                            UNPACKING = grdStockIn.Rows[i].Cells["UNPACKING"].Text,
                            LOCATIONNAME = grdStockIn.Rows[i].Cells["LOCATIONNAME"].Text,
                            EXCEPTION = grdStockIn.Rows[i].Cells["EXCEPTION"].Text,
                            DESCRIPTION = grdStockIn.Rows[i].Cells["DESCRIPTION"].Text,
                            XmlItemName = "DEVICEGROUP"
                        });
                    }
                }
                if (colDevice.Count <= 0)
                {
                    txtResultComment.Text = "Please check the data!";
                    return;
                }
                YARD_DEVICE objFlag = new YARD_DEVICE();
                objFlag.SITENAME = ConnectionInfo.SiteName;
                if (mesCommonService.SetEvent("YardStockIn", new object[] { objFlag, new MesList<MesList<YARD_DEVICE>>() { colDevice } }))
                {
                    txtResultComment.Text = "Operation successful";
                    this.ViewData();
                }

The predecessor created a MesList first, and MesList was defined by himself.

 public class MesList<T> : List<T>, IXmlEntity
    {
        private string xmlItemName;
        public string XmlItemName
        {
            get { return this.xmlItemName; }
            set { this.xmlItemName = value; }
        }
    }

This method is used for expansion, extending List. Because the transmission is in message mode, a label header is required. This ORIGINALDEVICELIST is the tag header under the body.

The rest is relatively simple, just add the selected information line to colDevice, record the quantity, and then package it.

3. Packed into XML for transmission

mesCommonService.SetEvent("YardStockIn", new object[] { objFlag, new MesList<MesList<YARD_DEVICE>>() { colDevice } })

This is the core of this code. Let’s take a closer look at SetEvent

public bool SetEvent(string eventName,object[] objParamList,bool requestFlag,double timeOut)
        {
            try
            {
                string response = msgUtil.SendRequestMessage(eventName,objParamList,requestFlag,timeOut);

                if(!requestFlag)
                {
                    return true;
                }

                if (string.IsNullOrEmpty(response))
                {
                    return false;
                }

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(response);

                return msgUtil.CheckErrorMessage(xmlDoc, false, true);// !msgUtil.HasError(ref xmlDoc);
            }
            catch (IDMMessageException messageex)
            {
                throw messageex;
            }
            catch (XmlException xmlex)
            {
                throw new Exception("Can't load xml document from the response message (" + eventName + ") or can't read the xml document.", xmlex);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 public string SendRequestMessage(string messageName, object[] objParameterCol,bool requestFlag,double requestTimeOut)
        {
            string responseMsg = null;
            try
            {
                ConnectionInfo.MessageName = messageName;
                ConnectionInfo.TransactionID = DateTime.Now.ToString(DATATIME_FORMAT_MSECOND);

                string sendMessage = "";

                sendMessage = CreateMessage(objParameterCol);

                if(requestFlag)
                {
                    // Log.
                    UILogger.This.Log(UILogger.Message, true, messageName, sendMessage);

                    if (requestTimeOut == 0)
                    {
                        responseMsg = MessageService.This.SendRequest(sendMessage);
                    }
                    else
                    {
                        responseMsg = MessageService.This.SendRequest(sendMessage, requestTimeOut);
                    }

                    // Raises an exception when the server can't reply properly or the response has problems we can't handle.
                    if (responseMsg == null || responseMsg.Length <= 0)
                    {
                        throw new EmptyReplyException("The reply message that MES server has sent is empty.")
                        {
                            LabelKey = "ReplyExceptionMessage",
                            MessageKey = "COMM30097",
                            MessageBody = "Client Send Message : " + sendMessage + "\
Reply Message Size Zero!!"
                        };
                    }
                }
                else
                {
                    MessageService.This.Send(sendMessage);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //ConnectionInfo.SourceSubject = string.Empty;
                //ConnectionInfo.TargetSubject = string.Empty;

                UILogger.This.Log(UILogger.Message, false, messageName, responseMsg);
            }

            return responseMsg;
        }
 private string CreateMessage(object[] objParaCollection)
        {
            XmlDocument rtnDoc = new XmlDocument();
            StringBuilder sBuilder = new StringBuilder();

            XmlWriterSettings xws = new XmlWriterSettings();
            xws.Encoding = Encoding.UTF8;
            xws.Indent = true;

            XmlWriter xWriter = XmlWriter.Create(sBuilder, xws);
            xWriter.WriteStartElement(xmlElementMessage);
            //Write Message's Header
            WriteMessageHeader(ref xWriter);

            xWriter.WriteStartElement(xmlElementMessageBody);
            if (objParaCollection != null & amp; & amp; objParaCollection.Count() > 0)
            {
                foreach (object objPara in objParaCollection)
                {
                    WriteBodyElement(ref xWriter, objPara);
                }
            }
            xWriter.WriteEndElement();

            xWriter.WriteEndElement();
            xWriter.Close();
            return sBuilder.ToString();
        }

After reading it carefully, I feel that the XML message format is very well written.

Four: Summary

Although the company’s technology is relatively old, there is still a lot to learn. Naturally, the current web version should all use json, which is much more convenient. . . . .