/// <summary>
        /// Build the protocol to xml string
        /// </summary>
        /// <returns>xml string</returns>
        public override string PackMessage()
        {
            StringBuilder xmlSB = new StringBuilder();
            RestaurantEntity.NewAccountDataTable loginTable = (RestaurantEntity.NewAccountDataTable)m_Param;

            xmlSB.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><message version=\"1.0\">");
            xmlSB.Append("<function>" + m_RequestCode + "</function><Data>");
            xmlSB.Append("<Sid>" + SessionID + "</Sid>");
            xmlSB.Append("<EmpUserName>" + loginTable[0].EmpUserName + "</EmpUserName>");
            xmlSB.Append("<EmpPassword>" + loginTable[0].EmpPassword + "</EmpPassword>");
            xmlSB.Append("</Data></message>");
            return xmlSB.ToString();
        }

        /// <summary>
        /// Parse the Returned xml string
        /// </summary>
        /// <param name="xmlString">Returned xml string</param>
        /// <returns>The object that been parse</returns>
        public override ResultValue ParseMessage(string xmlString)
        {
            ResultValue resultValue = new ResultValue();
            resultValue.SessionID = this.SessionID;
            resultValue.Param = this.m_Param;
            XmlDataDocument responseData = new XmlDataDocument();
            XmlElement element;

            try
            {
                responseData.LoadXml(xmlString);

                element = (XmlElement)responseData.GetElementsByTagName("Server")[0];
                if (element.GetAttribute("resNo") == ReturnFlag.Failed)
                {
                    resultValue.Flag = ReturnFlag.Failed;
                    resultValue.Data = new ServerReturnException(Info + element["ErrorCode"].InnerXml);
                    return resultValue;
                }

                RelationTable relation = new RelationTable();
                RestaurantEntity.NewAccountDataTable accountTable = new RestaurantEntity.NewAccountDataTable();
                RestaurantEntity.IDNameListDataTable idNameTable = new RestaurantEntity.IDNameListDataTable();
                relation.Master = accountTable;
                relation.Hypotaxis = idNameTable;

                RestaurantEntity.NewAccountRow accountRow = accountTable.NewNewAccountRow();
                accountRow.EmpID = element["EmpID"].InnerText.Trim();
                accountRow.EmpUserName = element["EmpName"].InnerText.Trim();
                accountRow.EmpPassword = element["Admin"].InnerText.Trim();
                foreach (XmlElement node in (element.GetElementsByTagName("FunctionID")))
                {
                    RestaurantEntity.IDNameListRow idNameRow = idNameTable.NewIDNameListRow();
                    idNameRow.ID = node.InnerText.Trim();
                    idNameTable.AddIDNameListRow(idNameRow);
                }
                accountTable.AddNewAccountRow(accountRow);

                resultValue.Flag = ReturnFlag.Successed;
                resultValue.Data = relation;
            }
            catch (Exception e)
            {
                resultValue.Flag = ReturnFlag.Failed;
                resultValue.Data = new ParsePacketException(Info + " " + e.Message);
            }

            return resultValue;
        }
    }

posted on 2009-03-23 15:30  rudyfung  阅读(144)  评论(0编辑  收藏  举报