using System;
using System.Collections;
using System.Collections.Generic;
using static System.Configuration.ConfigurationManager;
using System.Data;
using System.IO;
using System.Resources;
using System.Runtime.InteropServices;
using System.Web;
using System.Xml;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using static iMESCore.Base.iMESConst;

namespace EAIws_C
{

    static class modWS
    {
        #region --- Get Xml Value ---


        public static string GetXMLIdentityNode(XmlDocument xmlDoc, string SelectNode)
        {
            try
            {
                return xmlDoc.GetElementsByTagName("identity").Item(0).SelectNodes(SelectNode).Item(0).InnerXml;
            }
            catch (Exception e1)
            {
                return "IdentityError";
            }

        }

        public static string GetXMLTransactionID(ref XmlDocument xmlDoc)
        {
            try
            {
                return xmlDoc.GetElementsByTagName("identity").Item(0).SelectNodes("transactionid").Item(0).InnerXml;
            }
            catch (Exception e1)
            {
                // 發生錯誤時丟回本身的Function Name及系統錯誤訊息
                throw;
            } // New Exception("modWS.GetXMLCurUserNo: " & e1.Message)
        }

        public static string GetXMLModuleID(ref XmlDocument xmlDoc)
        {
            try
            {
                return xmlDoc.GetElementsByTagName("identity").Item(0).SelectNodes("moduleid").Item(0).InnerXml;
            }
            catch (Exception e1)
            {
                // 發生錯誤時丟回本身的Function Name及系統錯誤訊息
                throw;
            } // New Exception("modWS.GetXMLCurUserNo: " & e1.Message)
        }

        public static string GetXMLFunctionID(ref XmlDocument xmlDoc)
        {
            try
            {
                return xmlDoc.GetElementsByTagName("identity").Item(0).SelectNodes("functionid").Item(0).InnerXml;
            }
            catch (Exception e1)
            {
                // 發生錯誤時丟回本身的Function Name及系統錯誤訊息
                throw;
            } // New Exception("modWS.GetXMLCurUserNo: " & e1.Message)
        }

        public static string GetXMLComputerName(ref XmlDocument xmlDoc)
        {
            try
            {
                return xmlDoc.GetElementsByTagName("identity").Item(0).SelectNodes("computername").Item(0).InnerXml;
            }
            catch (Exception e1)
            {
                // 發生錯誤時丟回本身的Function Name及系統錯誤訊息
                throw;
            } // New Exception("modWS.GetXMLCurUserNo: " & e1.Message)
        }

        public static string GetXMLCurUserNo(ref XmlDocument xmlDoc)
        {
            try
            {
                return xmlDoc.GetElementsByTagName("identity").Item(0).SelectNodes("curuserno").Item(0).InnerXml;
            }
            catch (Exception e1)
            {
                // 發生錯誤時丟回本身的Function Name及系統錯誤訊息
                throw;
            } // New Exception("modWS.GetXMLCurUserNo: " & e1.Message)
        }

        public static string GetXMLSendTime(ref XmlDocument xmlDoc)
        {
            try
            {
                return xmlDoc.GetElementsByTagName("identity").Item(0).SelectNodes("sendtime").Item(0).InnerXml;
            }
            catch (Exception e1)
            {
                // 發生錯誤時丟回本身的Function Name及系統錯誤訊息
                throw;
            } // New Exception("modWS.GetXMLCurUserNo: " & e1.Message)
        }

        public static string GetExceptionSysMsg(ref XmlDocument Xmldoc)
        {
            string GetExceptionSysMsgRet = default(string);
            GetExceptionSysMsgRet = CInput(Xmldoc.DocumentElement.GetElementsByTagName("sysmsg").Item(0).InnerXml);
            return GetExceptionSysMsgRet;
        }

        public static string GetExceptionMesMsg(ref XmlDocument Xmldoc)
        {
            string GetExceptionMesMsgRet = default(string);
            GetExceptionMesMsgRet = CInput(Xmldoc.DocumentElement.GetElementsByTagName("mesmsg").Item(0).InnerXml);
            return GetExceptionMesMsgRet;
        }

        public static bool chkExecutionSuccess(ref XmlDocument Xmldoc)
        {
            bool chkExecutionSuccessRet = default(bool);
            if (Xmldoc.DocumentElement["result"].InnerXml == "success")
            {
                chkExecutionSuccessRet = true;
            }
            else
            {
                chkExecutionSuccessRet = false;
            }

            return chkExecutionSuccessRet;
        }


        #endregion

        #region --- Combine Indentity Xml ---

        public static string CombineXMLIdentity(XmlDocument XMLDoc)
        {
            string CombineXMLIdentityRet = default(string);
            CombineXMLIdentityRet = Conversions.ToString('\t') + '\t' + "<transactionid>" + GetXMLIdentityNode(XMLDoc, "transactionid") + "</transactionid>" + '\n';
            CombineXMLIdentityRet += Conversions.ToString('\t') + '\t' + "<moduleid>" + GetXMLIdentityNode(XMLDoc, "moduleid") + "</moduleid>" + '\n';
            CombineXMLIdentityRet += Conversions.ToString('\t') + '\t' + "<functionid>" + GetXMLIdentityNode(XMLDoc, "functionid") + "</functionid>" + '\n';
            CombineXMLIdentityRet += Conversions.ToString('\t') + '\t' + "<computername>" + GetXMLIdentityNode(XMLDoc, "computername") + "</computername>" + '\n';
            CombineXMLIdentityRet += Conversions.ToString('\t') + '\t' + "<curuserno>" + GetXMLIdentityNode(XMLDoc, "curuserno") + "</curuserno>" + '\n';
            CombineXMLIdentityRet += Conversions.ToString('\t') + '\t' + "<sendtime>" + GetXMLIdentityNode(XMLDoc, "sendtime") + "</sendtime>";
            return CombineXMLIdentityRet;
        }

        public static string CombineXMLIdentity(ref string ComputerName, ref string CurUserNo, ref string SendTime)
        {
            string CombineXMLIdentityRet = default(string);
            CombineXMLIdentityRet = "<computername>" + ComputerName + "</computername>" + "<curuserno>" + CurUserNo + "</curuserno>" + "<sendtime>" + SendTime + "</sendtime>";
            return CombineXMLIdentityRet;
        }

        #endregion

        #region --- Combine Input Xml ---


        public static string CombineXMLIdentity(ref string TransactionID, ref string ModuleID, ref string FunctionID, ref string ComputerName, ref string CurUserNo, ref string SendTime)
        {
            string CombineXMLIdentityRet = default(string);
            CombineXMLIdentityRet = "<transactionid>" + TransactionID + "</transactionid>" + "<moduleid>" + ModuleID + "</moduleid>" + "<functionid>" + FunctionID + "</functionid>" + "<computername>" + ComputerName + "</computername>" + "<curuserno>" + CurUserNo + "</curuserno>" + "<sendtime>" + SendTime + "</sendtime>";
            return CombineXMLIdentityRet;
        }

        public static string CombineXMLParameter(ref string value_name, ref string name, ref string type, ref string value, ref string desc)
        {
            string CombineXMLParameterRet = default(string);

            CombineXMLParameterRet = "<" + value_name.ToLower() + ">" + "<name>" + name + "</name>" + "<type>" + type + "</type>" + "<value>" + value + "</value>" + "<desc>" + desc + "</desc>" + "</" + value_name.ToLower() + ">";
            return CombineXMLParameterRet;

        }

        public static string CombineXMLParameter(ref string name, ref string type, ref string value, ref string desc)
        {
            string CombineXMLParameterRet = default(string);
            CombineXMLParameterRet = "<" + name.ToLower() + ">" + "<name>" + name + "</name>" + "<type>" + type + "</type>" + "<value>" + value + "</value>" + "<desc>" + desc + "</desc>" + "</" + name.ToLower() + ">";
            return CombineXMLParameterRet;
        }

        public static string CombineXMLValue(ref string TagName, ref string Value)
        {
            string CombineXMLValueRet = default(string);
            CombineXMLValueRet = "<" + TagName + ">" + Value + "</" + TagName + ">";
            return CombineXMLValueRet;
        }

        public static string CombineXMLValueTag(ref string Value)
        {
            string CombineXMLValueTagRet = default(string);
            CombineXMLValueTagRet = "<value>" + Value + "</value>";
            return CombineXMLValueTagRet;
        }

        public static string CombineXMLRequest(ref string strIdentity, ref string strParameter)
        {
            string CombineXMLRequestRet = default(string);

            CombineXMLRequestRet = "<request>" + "<identity>" + strIdentity + "</identity>";
            if (!string.IsNullOrEmpty(strParameter))
            {
                CombineXMLRequestRet = CombineXMLRequestRet + "<parameter>" + strParameter + "</parameter>";
            }
            CombineXMLRequestRet = CombineXMLRequestRet + "</request>";
            return CombineXMLRequestRet;

        }

        public static string CombineXMLParameterMultiValue(ref string value_name, ref string name, ref string type, ref string value, ref string desc)
        {
            string CombineXMLParameterMultiValueRet = default(string);
            // Value不用加上Tag
            CombineXMLParameterMultiValueRet = "<" + value_name.ToLower() + ">" + "<name>" + name + "</name>" + "<type>" + type + "</type>" + value + "<desc>" + desc + "</desc>" + "</" + value_name.ToLower() + ">";
            return CombineXMLParameterMultiValueRet;
        }

        public static string CombineXMLAdditional(ref string strAdditional)
        {
            string CombineXMLAdditionalRet = default(string);
            CombineXMLAdditionalRet = "<additional>" + strAdditional;
            CombineXMLAdditionalRet = CombineXMLAdditionalRet + "</additional>";
            return CombineXMLAdditionalRet;
        }

        public static string CombineAddXML_Add(ref string name, ref string type, ref string value)
        {
            string CombineAddXML_AddRet = default(string);
            CombineAddXML_AddRet = "<field>" + "<name>" + name + "</name>" + "<type>" + type + "</type>" + "<value>" + value + "</value>" + "</field>";
            return CombineAddXML_AddRet;
        }

        public static string CombineAddXML_Edit(ref string name, ref string type, ref string value)
        {
            string CombineAddXML_EditRet = default(string);
            CombineAddXML_EditRet = "<field>" + "<name>" + name + "</name>" + "<type>" + type + "</type>" + "<value>" + value + "</value>" + "</field>";
            return CombineAddXML_EditRet;
        }

        public static string CombineAddXML_Field(ref string name)
        {
            string CombineAddXML_FieldRet = default(string);
            CombineAddXML_FieldRet = "<field>" + "<name>" + name + "</name>" + "</field>";
            return CombineAddXML_FieldRet;
        }

        public static string CombineAddXML_Condition(string condition)
        {
            string CombineAddXML_ConditionRet = default(string);

            // 2007/03/22,sammi.呼叫CombineAddXML_Condition時,不可加CInput.
            // 因CInput會將單引號轉成二個單引號,若Condition為下述範例時,會Error.
            // strAdditional = CombineXMLAdditional(CombineAddXML_Condition("ModuleType in ('REWORK','SLR')"))
            // strParameter += strAdditional

            CombineAddXML_ConditionRet = "<condition>" + condition + "</condition>";
            return CombineAddXML_ConditionRet;

        }

        public static string CInput(string strInput)
        {
            string CInputRet = default(string);
            // 將傳入值內的單引號轉換為可存入資料庫的格式
            // 將傳入值內的 &, >, < 三個特殊字元轉換為XmlDocument可解譯之代替符號                  
            // 傳入值: strInput包含特殊字元的字串
            // 傳回值: 將特殊字元變更為代替符號的字串

            // 轉換 ' 為 '' (單引號轉為兩個單引號)
            CInputRet = Strings.Replace(strInput, "'", "''");

            // 轉換 & 為 &amp;
            CInputRet = Strings.Replace(CInputRet, "&", "&amp;");

            // CInput = Replace(CInput, """", "''")   'AddFlow的Xml字串不可將雙引號轉為兩個單引號,XMLToFlow會Error

            // 轉換 > 為 &gt;
            CInputRet = Strings.Replace(CInputRet, ">", "&gt;");

            // 轉換 < 為 &lt;
            CInputRet = Strings.Replace(CInputRet, "<", "&lt;");
            return CInputRet;

        }

        #endregion

        #region --- Combine Output Xml ---

        public static bool ChgTranslateException(List<ArrayList> Exception, ref string strExceptionSysmsg, ref string strExceptionMesmsg, ref string strExceptionStack)
        {
            bool ChgTranslateExceptionRet = default(bool);

            try
            {
                foreach (ArrayList al in Exception)
                {
                    if (!string.IsNullOrEmpty(strExceptionSysmsg))
                        strExceptionSysmsg += Conversions.ToString('\n');
                    if (string.IsNullOrEmpty(Conversions.ToString(al[1])) == false)
                        strExceptionSysmsg += Conversions.ToString('\t') + '\t' + '\t' + al[1].ToString();
                    if (string.IsNullOrEmpty(strExceptionMesmsg) == false)
                        strExceptionMesmsg += Conversions.ToString('\n');
                    if (string.IsNullOrEmpty(Conversions.ToString(al[2])) == false)
                    {
                        // 翻譯錯誤訊息
                        string strErrName_Rep = "";
                        string ErrorName = al[2].ToString();
                        if (Strings.InStr(ErrorName, "[%", CompareMethod.Text) > 0)
                        {
                            string strKey, strKeyValue;
                            int intStart, j, k, l;

                            intStart = 1;
                            j = 1;

                            while (j != 0)
                            {
                                j = Strings.InStr(intStart, ErrorName, "[%", CompareMethod.Text);
                                if (j == 0)
                                {
                                    strErrName_Rep += Strings.Mid(ErrorName, intStart);
                                }
                                else
                                {
                                    k = Strings.InStr(j + 2, ErrorName, "%]", CompareMethod.Text);
                                    if (k == 0)
                                    {
                                        // 找不到對應的結束字元.
                                        strErrName_Rep += Strings.Mid(ErrorName, intStart);
                                        j = 0;
                                    }
                                    else
                                    {
                                        // 檢查是否有不對稱的情況.
                                        l = Strings.InStr(j + 2, ErrorName, "[%", CompareMethod.Text);
                                        if (l == 0 || l > k)
                                        {
                                            strErrName_Rep += Strings.Mid(ErrorName, intStart, j - intStart);
                                            strKey = Strings.Mid(ErrorName, j + 2, k - j - 2);
                                            // 將Key執行語系轉換
                                            try
                                            {
                                                strKeyValue = "";
                                                strKeyValue = TranslateMsg(strKey);
                                            }
                                            catch (Exception ex)
                                            {
                                                strKeyValue = strKey;
                                            }
                                            if (string.IsNullOrEmpty(strKeyValue))
                                            {
                                                strErrName_Rep += strKey;
                                            }
                                            else
                                            {
                                                strErrName_Rep += strKeyValue;
                                            } // & "(" & strKey & ")"
                                              // 下次截取字串的起始位置
                                            intStart = k + 2;
                                        }
                                        else
                                        {
                                            // 不對稱,找到最內層的[%%]
                                            int m;
                                            // 在前面的條件下,一定找得到.
                                            m = Strings.InStrRev(ErrorName, "[%", k, CompareMethod.Text);
                                            // 擷出不轉換的字串.
                                            strErrName_Rep += Strings.Mid(ErrorName, intStart, m - intStart);
                                            strKey = Strings.Mid(ErrorName, m + 2, k - m - 2);
                                            // 將Key執行語系轉換
                                            try
                                            {
                                                strKeyValue = "";
                                                strKeyValue = TranslateMsg(strKey);
                                            }
                                            catch (Exception ex)
                                            {
                                                strKeyValue = strKey;
                                            }
                                            if (string.IsNullOrEmpty(strKeyValue))
                                            {
                                                strErrName_Rep += strKey;
                                            }
                                            else
                                            {
                                                strErrName_Rep += strKeyValue;
                                            } // & "(" & strKey & ")"
                                              // 下次截取字串的起始位置
                                            intStart = k + 2;
                                        }
                                    }
                                }
                            }
                        }
                        // strExceptionMesmsg += Chr(9) & Chr(9) & Chr(9) & Resources.Resource.ResourceManager.GetString(al.Item(2).ToString) & "(" & al.Item(2).ToString & ")"
                        strExceptionMesmsg += Conversions.ToString('\t') + '\t' + '\t' + strErrName_Rep;
                    }

                    if (!string.IsNullOrEmpty(strExceptionStack))
                        strExceptionStack += Conversions.ToString('\n');
                    if (string.IsNullOrEmpty(Conversions.ToString(al[3])) == false)
                        strExceptionStack += Conversions.ToString('\t') + '\t' + '\t' + al[3].ToString();
                }

                ChgTranslateExceptionRet = true;
            }

            catch (Exception e1)
            {
                throw;
                ChgTranslateExceptionRet = false;
            }

            return ChgTranslateExceptionRet;

        }

        public static bool ChgTranslateException_ByLanguage(ref List<ArrayList> Exception, ref string strExceptionSysmsg, ref string strExceptionMesmsg, ref string strExceptionStack, bool ShowStack)
        {
            bool ChgTranslateException_ByLanguageRet = default(bool);

            try
            {
                // item content>> 0:code, 1:sysmsg, 2:mesmsg, 3:stack
                foreach (ArrayList al in Exception)
                {
                    if (!string.IsNullOrEmpty(strExceptionSysmsg))
                        strExceptionSysmsg += Conversions.ToString(ControlChars.Cr);
                    if (!string.IsNullOrEmpty(strExceptionStack))
                        strExceptionStack += Conversions.ToString(ControlChars.Cr);
                    // code
                    if (string.IsNullOrEmpty(Conversions.ToString(al[0])) == false)
                    {
                        strExceptionSysmsg += string.Format("[{0}]", TranslateMsg(al[0].ToString()));
                    }
                    if (string.IsNullOrEmpty(Conversions.ToString(al[1])) == false)
                    {
                        strExceptionSysmsg += "," + TranslateMsg(al[1].ToString());
                    }
                    if (string.IsNullOrEmpty(strExceptionMesmsg) == false)
                        strExceptionMesmsg += Conversions.ToString(ControlChars.Cr);
                    if (string.IsNullOrEmpty(Conversions.ToString(al[2])) == false)
                    {
                        // 翻譯錯誤訊息
                        string strErrName_Rep = "";
                        string ErrorName = al[2].ToString();
                        strErrName_Rep = TranslateMsg(ErrorName);
                        strExceptionMesmsg += "," + strErrName_Rep;
                    }
                    // stack
                    if (ShowStack == true && string.IsNullOrEmpty(Conversions.ToString(al[3])) == false)
                    {
                        strExceptionStack += ControlChars.Tab + al[3].ToString();
                    }
                }

                ChgTranslateException_ByLanguageRet = true;
            }

            catch (Exception ex)
            {
                throw;
                ChgTranslateException_ByLanguageRet = false;
            }

            return ChgTranslateException_ByLanguageRet;

        }

        public static bool ChgTranslateException_ByLanguage(ref List<ArrayList> Exception, ref string strExceptionSysmsg, ref string strExceptionMesmsg, ref string strExceptionStack)
        {
            bool ChgTranslateException_ByLanguageRet = default(bool);

            try
            {
                foreach (ArrayList al in Exception)
                {
                    if (!string.IsNullOrEmpty(strExceptionSysmsg))
                        strExceptionSysmsg += Conversions.ToString('\n');
                    if (string.IsNullOrEmpty(Conversions.ToString(al[1])) == false)
                        strExceptionSysmsg += Conversions.ToString('\t') + '\t' + '\t' + TranslateMsg(al[1].ToString());
                    if (string.IsNullOrEmpty(strExceptionMesmsg) == false)
                        strExceptionMesmsg += Conversions.ToString('\n');
                    if (string.IsNullOrEmpty(Conversions.ToString(al[2])) == false)
                    {
                        // 翻譯錯誤訊息
                        string strErrName_Rep = "";
                        string ErrorName = al[2].ToString();
                        strErrName_Rep = TranslateMsg(ErrorName);
                        strExceptionMesmsg += Conversions.ToString('\t') + '\t' + '\t' + strErrName_Rep;
                    }

                    if (!string.IsNullOrEmpty(strExceptionStack))
                        strExceptionStack += Conversions.ToString('\n');
                    if (string.IsNullOrEmpty(Conversions.ToString(al[3])) == false)
                        strExceptionStack += Conversions.ToString('\t') + '\t' + '\t' + al[3].ToString();
                }

                ChgTranslateException_ByLanguageRet = true;
            }

            catch (Exception e1)
            {
                throw;
                ChgTranslateException_ByLanguageRet = false;
            }

            return ChgTranslateException_ByLanguageRet;

        }

        public static bool ChgTranslateMessage(ref List<ArrayList> Message, ref string strMmsg)
        {
            bool ChgTranslateMessageRet = default(bool);

            try
            {
                foreach (ArrayList al in Message)
                {
                    if (!string.IsNullOrEmpty(strMmsg))
                        strMmsg += Conversions.ToString('\n');
                    if (string.IsNullOrEmpty(Conversions.ToString(al[2])) == false)
                    {
                        // 翻譯訊息
                        string strErrName_Rep = "";
                        string ErrorName = al[2].ToString();
                        if (Strings.InStr(ErrorName, "[%", CompareMethod.Text) > 0)
                        {
                            // Replace[%%]
                            string strKey, strKeyValue;
                            int intStart, j, k, l;

                            intStart = 1;
                            j = 1;

                            while (j != 0)
                            {
                                j = Strings.InStr(intStart, ErrorName, "[%", CompareMethod.Text);
                                if (j == 0)
                                {
                                    strErrName_Rep += Strings.Mid(ErrorName, intStart);
                                }
                                else
                                {
                                    k = Strings.InStr(j + 2, ErrorName, "%]", CompareMethod.Text);
                                    if (k == 0)
                                    {
                                        // 找不到對應的結束字元.
                                        strErrName_Rep += Strings.Mid(ErrorName, intStart);
                                        j = 0;
                                    }
                                    else
                                    {
                                        // 檢查是否有不對稱的情況.
                                        l = Strings.InStr(j + 2, ErrorName, "[%", CompareMethod.Text);
                                        if (l == 0 || l > k)
                                        {
                                            strErrName_Rep += Strings.Mid(ErrorName, intStart, j - intStart);
                                            strKey = Strings.Mid(ErrorName, j + 2, k - j - 2);
                                            // 將Key執行語系轉換
                                            try
                                            {
                                                strKeyValue = "";
                                                strKeyValue = TranslateMsg(strKey);
                                            }
                                            catch (Exception ex)
                                            {
                                                strKeyValue = strKey;
                                            }
                                            if (string.IsNullOrEmpty(strKeyValue))
                                            {
                                                strErrName_Rep += strKey;
                                            }
                                            else
                                            {
                                                strErrName_Rep += strKeyValue;
                                            } // & "(" & strKey & ")"
                                              // 下次截取字串的起始位置
                                            intStart = k + 2;
                                        }
                                        else
                                        {
                                            // 不對稱,找到最內層的[%%]
                                            int m;
                                            // 在前面的條件下,一定找得到.
                                            m = Strings.InStrRev(ErrorName, "[%", k, CompareMethod.Text);
                                            // 擷出不轉換的字串.
                                            strErrName_Rep += Strings.Mid(ErrorName, intStart, m - intStart);
                                            strKey = Strings.Mid(ErrorName, m + 2, k - m - 2);
                                            // 將Key執行語系轉換
                                            try
                                            {
                                                strKeyValue = "";
                                                strKeyValue = TranslateMsg(strKey);
                                            }
                                            catch (Exception ex)
                                            {
                                                strKeyValue = strKey;
                                            }
                                            if (string.IsNullOrEmpty(strKeyValue))
                                            {
                                                strErrName_Rep += strKey;
                                            }
                                            else
                                            {
                                                strErrName_Rep += strKeyValue;
                                            } // & "(" & strKey & ")"
                                              // 下次截取字串的起始位置
                                            intStart = k + 2;
                                        }
                                    }
                                }
                            }
                        }

                        // strMmsg += Resources.Resource.ResourceManager.GetString(al.Item(2).ToString) & "(" & al.Item(2).ToString & ")"
                        strMmsg += strErrName_Rep;
                    }

                }

                ChgTranslateMessageRet = true;
            }

            catch (Exception e1)
            {
                throw;

                ChgTranslateMessageRet = false;
            }

            return ChgTranslateMessageRet;

        }

        public static string CombineXMLReturnValue(ref string value_name, ref string name, ref string type, ref string schema, ref string value, ref string desc)
        {
            string CombineXMLReturnValueRet = default(string);

            try
            {
                CombineXMLReturnValueRet = "<" + value_name.Trim().ToLower() + ">" + "<name>" + name + "</name>" + "<type>" + type + "</type>" + "<schema>" + schema + "</schema>" + "<value>" + value + "</value>" + "<desc>" + desc + "</desc>" + "</" + value_name.Trim().ToLower() + ">";
            }

            catch (Exception e1)
            {
                // 發生錯誤時丟回本身的Function Name及系統錯誤訊息
                throw;

            } // New Exception("modWS.CombineXMLReturnValue: " & e1.Message)

            return CombineXMLReturnValueRet;

        }

        public static string CombineXMLException(ref string code, ref string sysmsg, ref string mesmsg, string stack = "")
        {
            string CombineXMLExceptionRet = default(string);

            // Edit by Yun 2006.8.9   加上 CInput
            try
            {
                CombineXMLExceptionRet = "<code>" + code + "</code>" + "<sysmsg>" + CInput(sysmsg) + "</sysmsg>" + "<mesmsg>" + CInput(mesmsg) + "</mesmsg>" + "<stack>" + CInput(stack) + "</stack>";
            }

            catch (Exception e1)
            {
                // 發生錯誤時丟回本身的Function Name及系統錯誤訊息
                throw;

            } // New Exception("modWS.CombineXMLException: " & e1.Message)

            return CombineXMLExceptionRet;

        }

        public static string CombineXMLMessage(string code, string type, string msg)
        {
            string CombineXMLMessageRet = default(string);

            try
            {
                CombineXMLMessageRet = "<note>" + "<mcode>" + code + "</mcode>" + "<mtype>" + type + "</mtype>" + "<mmsg>" + msg + "</mmsg>" + "</note>";
            }
            catch (Exception e1)
            {
                // 發生錯誤時丟回本身的Function Name及系統錯誤訊息
                throw;

            } // New Exception("modWS.CombineXMLMessage: " & e1.Message)

            return CombineXMLMessageRet;

        }

        public static string CombineXMLResponse(string Identity, string ReturnValue = null, List<ArrayList> Exception = null, string Result = null, List<ArrayList> Message = null)
        {
            string CombineXMLResponseRet = default(string);

            try
            {
                // <response>
                CombineXMLResponseRet = "<response>" + '\n';

                // <identity>
                if (string.IsNullOrEmpty(Identity))
                {
                    CombineXMLResponseRet += '\t' + "<identity></identity>" + '\n';
                }
                else
                {
                    CombineXMLResponseRet += '\t' + "<identity>" + '\n';
                    CombineXMLResponseRet += Identity + '\n';
                    CombineXMLResponseRet += '\t' + "</identity>" + '\n';
                }

                // <returnvalue>
                if (string.IsNullOrEmpty(ReturnValue))
                {
                    CombineXMLResponseRet += '\t' + "<returnvalue></returnvalue>" + '\n';
                }
                else
                {
                    CombineXMLResponseRet += '\t' + "<returnvalue>" + '\n';
                    CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + ReturnValue + '\n';
                    CombineXMLResponseRet += '\t' + "</returnvalue>" + '\n';
                }

                // <result>
                if (string.IsNullOrEmpty(Result))
                {
                    CombineXMLResponseRet += '\t' + "<result></result>" + '\n';
                }
                else
                {
                    CombineXMLResponseRet += '\t' + "<result>" + Result + "</result>" + '\n';
                }

                // <exception>
                string strExceptionSysmsg = string.Empty;
                string strExceptionMesmsg = string.Empty;
                string strExceptionStack = string.Empty;

                if (Exception.Count == 0)
                {
                    CombineXMLResponseRet += '\t' + "<exception></exception>" + '\n';
                }
                else
                {
                    // 2016-12-01, Joe, Exception轉語系
                    ChgTranslateException(Exception, ref strExceptionSysmsg, ref strExceptionMesmsg, ref strExceptionStack);

                    CombineXMLResponseRet += '\t' + "<exception>" + '\n';
                    CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<code>" + AppSettings["defWSErrCode"] + "</code>" + '\n';
                    if (string.IsNullOrEmpty(strExceptionSysmsg) == true)
                    {
                        CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<sysmsg></sysmsg>" + '\n';
                    }
                    else
                    {
                        CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<sysmsg>" + '\n' + strExceptionSysmsg + '\n' + '\t' + '\t' + "</sysmsg>" + '\n';
                    }
                    if (string.IsNullOrEmpty(strExceptionMesmsg) == true)
                    {
                        CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<mesmsg>" + strExceptionMesmsg + "</mesmsg>" + '\n';
                    }
                    else
                    {
                        CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<mesmsg>" + '\n' + strExceptionMesmsg + '\n' + '\t' + '\t' + "</mesmsg>" + '\n';
                    }
                    if (string.IsNullOrEmpty(strExceptionStack))
                    {
                        CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<stack></stack>" + '\n';
                    }
                    else
                    {
                        CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<stack>" + '\n' + strExceptionStack + '\n' + '\t' + '\t' + "</stack>" + '\n';
                    }
                    CombineXMLResponseRet += '\t' + "</exception>" + '\n';
                }

                // <message>
                string strMmsg = string.Empty;

                if (Message.Count == 0)
                {
                    CombineXMLResponseRet += '\t' + "<message></message>" + '\n';
                }
                else
                {
                    CombineXMLResponseRet += '\t' + "<message>" + '\n';
                    // 2016-12-01, Joe, Message轉語系
                    // Call ChgTranslateMessage(Message, strMmsg)

                    CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<mcode>" + AppSettings["defMSGCode"] + "</mcode>" + '\n';
                    CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<mtype>" + AppSettings["defMSGType"] + "</mtype>" + '\n';
                    CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<mmsg>" + strMmsg + "</mmsg>" + '\n';
                    CombineXMLResponseRet += '\t' + "</message>" + '\n';
                }

                CombineXMLResponseRet += "</response>" + '\n';
            }

            catch (Exception e1)
            {
                // 發生錯誤時丟回本身的Function Name及系統錯誤訊息
                // Throw 'New Exception("modWS.CombineXMLResponse: " & e1.Message)
                CombineXMLResponseRet = "<response>" + '\n';
                CombineXMLResponseRet += '\t' + "<identity></identity>" + '\n';
                CombineXMLResponseRet += '\t' + "<exception>" + '\n';
                CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<code></code>" + '\n';
                CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<sysmsg>" + '\n';
                CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + '\t' + e1.Message + '\n';
                CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "</sysmsg>" + '\n';
                CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<mesmsg>" + '\n';
                CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + '\t' + "CombineXMLResponse;Fail" + '\n';
                CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "</mesmsg>" + '\n';
                CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "<stack>" + '\n';
                CombineXMLResponseRet += e1.StackTrace + '\n';
                CombineXMLResponseRet += Conversions.ToString('\t') + '\t' + "</stack>" + '\n';
                CombineXMLResponseRet += '\t' + "</exception>" + '\n';
                CombineXMLResponseRet += '\t' + "<message></message>" + '\n';
                CombineXMLResponseRet += "</response>";
            }

            return CombineXMLResponseRet;

        }

        public static string CombineXMLResponse(ref string Identity, ref string ReturnValue, ref string Exception, ref string Result, ref string Message)
        {
            string CombineXMLResponseRet = default(string);

            try
            {
                CombineXMLResponseRet = "<response><identity>" + Identity + "</identity>";

                if (!string.IsNullOrEmpty(ReturnValue))
                {
                    CombineXMLResponseRet = CombineXMLResponseRet + "<returnvalue>" + ReturnValue + "</returnvalue>";
                }

                if (!string.IsNullOrEmpty(Exception))
                {
                    CombineXMLResponseRet = CombineXMLResponseRet + "<exception>" + Exception + "</exception>";
                }

                CombineXMLResponseRet = CombineXMLResponseRet + "<result>" + Result + "</result>";

                if (!string.IsNullOrEmpty(Message))
                {
                    CombineXMLResponseRet = CombineXMLResponseRet + "<message>" + Message + "</message>";
                }

                CombineXMLResponseRet = CombineXMLResponseRet + "</response>";
            }

            catch (Exception e1)
            {
                // 發生錯誤時丟回本身的Function Name及系統錯誤訊息
                throw;

            } // New Exception("modWS.CombineXMLResponse: " & e1.Message)

            return CombineXMLResponseRet;

        }

        // 2016-02-03, Joe, 調整Response XML架構

        public static string CombineXMLResponse_Parameter(string key = defString, string type = defString, DataTable[] dtTemp = null, bool IsDetail = false)
        {
            string CombineXMLResponse_ParameterRet = default(string);

            int i = 0;
            var XmlDoc = new XmlDocument();
            XmlElement nodeParameter = null;
            if ((key ?? "") != defString | (type ?? "") != defString)
            {
                nodeParameter = XmlDoc.CreateElement("parameter");
                if ((key ?? "") != defString)
                    nodeParameter.SetAttribute("key", key);
                if ((type ?? "") != defString)
                    nodeParameter.SetAttribute("type", type);
                XmlDoc.AppendChild(nodeParameter);
            }

            if (!(dtTemp[0] == null))
            {
                // For iTable As Integer = 0 To dtTemp.Length - 1
                // If dtTemp(iTable) Is Nothing Then Continue For
                i = 0;

                XmlElement nodeData;
                XmlElement nodeDetail = null;
                XmlElement nodeTemp;
                if (IsDetail == false)
                {
                    nodeData = XmlDoc.CreateElement("data");
                    nodeTemp = nodeData;
                }
                else
                {
                    nodeDetail = XmlDoc.CreateElement("detail");
                    nodeTemp = nodeDetail;
                }

                // 除第一個table外其他視為第二階
                nodeTemp.SetAttribute("name", dtTemp[0].TableName);
                XmlElement nodeField;
                foreach (DataRow dr in dtTemp[0].Rows)
                {

                    var nodeRow = XmlDoc.CreateElement("row");
                    i = i + 1;
                    nodeRow.SetAttribute("seq", i.ToString());
                    foreach (DataColumn dc in dtTemp[0].Columns)
                    {
                        nodeField = XmlDoc.CreateElement("field");
                        nodeField.SetAttribute("name", dc.ColumnName.ToLower());
                        nodeField.SetAttribute("type", "string");
                        nodeField.InnerText = dr[dc].ToString();
                        nodeRow.AppendChild(nodeField);
                    }
                    if (IsDetail == false)
                    {
                        for (int iTable = 1, loopTo = dtTemp.Length - 1; iTable <= loopTo; iTable++)
                            CombineXMLResponse_Parameter(dtTemp: new DataTable[] { dtTemp[iTable] }, IsDetail: true);
                    }
                    nodeTemp.AppendChild(nodeRow);
                }
                if (IsDetail == false)
                {
                    nodeParameter.AppendChild(nodeTemp);
                }
                else
                {
                    nodeDetail.AppendChild(nodeTemp);
                }
                // Next
            }

            CombineXMLResponse_ParameterRet = XmlDoc.InnerXml;
            return CombineXMLResponse_ParameterRet;

        }

        public static string FormatXMLSchema(ref string XmlSchema)
        {
            string FormatXMLSchemaRet = default(string);

            int intTruncateLength;

            try
            {
                FormatXMLSchemaRet = "";

                // 將Schema字串的前面一行<?...........?>刪除，以符合XML格式
                intTruncateLength = Strings.InStr(XmlSchema, "?>") + 1;
                if (intTruncateLength != 1)
                {
                    FormatXMLSchemaRet = Strings.Right(XmlSchema, Strings.Len(XmlSchema) - intTruncateLength);
                }
                else
                {
                    FormatXMLSchemaRet = XmlSchema;
                }
            }

            catch (Exception e1)
            {
                // 發生錯誤時丟回本身的Function Name及系統錯誤訊息
                throw;

            } // New Exception("modWS.FormatXMLSchema: " & e1.Message)

            return FormatXMLSchemaRet;

        }

        #endregion

        #region --- for Message Change Resource use---

        /// <summary>
        /// 2020/05/17 OwenLiu, Mantis:0071895, 讀取語系檔案到 Dictionary
        /// </summary>
        /// <param name="ResourceFilePath"></param>
        /// <returns></returns>
        private static Dictionary<string, string> funReadResourceFile_Dictionary(string ResourceFilePath)
        {

            var oReturnResource = new Dictionary<string, string>();

            try
            {
                var reader = new ResourceReader(ResourceFilePath);
                var en = reader.GetEnumerator();

                while (en.MoveNext())
                    oReturnResource.Add(Conversions.ToString(en.Key), Conversions.ToString(en.Value));
                reader.Close();
            }
            catch (Exception ex)
            {
                throw;
            }

            return oReturnResource;

        }

        /// <summary>
        /// 2020/05/15 OwenLiu, Mantis:0071895, 增加多語系檔案的語系轉換機制
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="pKey"></param>
        /// <param name="pResValue"></param>
        /// <returns></returns>
        private static bool funGetResouceValueText(string sourceFile, string pKey, ref string pResValue)
        {

            bool blnResult = false;
            ResourceSet rmGen = null;

            try
            {
                using (var fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    rmGen = new ResourceSet(fs);
                    fs.Close();
                }

                pResValue = rmGen.GetString(pKey.ToUpper());
                if (!string.IsNullOrEmpty(pResValue))
                    blnResult = true;
            }
            catch (Exception ex)
            {
                throw;
            }

            return blnResult;

        }

        /// <summary>
        /// 2020/05/15 OwenLiu, Mantis:0071895, 取回所有語系檔案的 ResourceManager Array
        /// </summary>
        /// <param name="pResourcePath"></param>
        /// <param name="pLanguageMode"></param>
        /// <param name="oResourceManager"></param>
        /// <returns></returns>
        private static bool funGetMultiResourceManager(string pResourcePath, string pLanguageMode, ref ResourceManager oResourceManager, ref Dictionary<string, string>[] oResourceDictionary)
        {

            bool blnResult = false;
            string strResourceFile = "mesresource";

            try
            {

                if (!string.IsNullOrEmpty(pLanguageMode))
                {
                    strResourceFile += "." + pLanguageMode;
                }
                else
                {
                    strResourceFile += ".en";
                }

                if (new DirectoryInfo(pResourcePath).Exists == false)
                {
                    oResourceManager = null;
                    return blnResult;
                }

                // 一次性建立多個ResourceManager, 以節省控制項的語系轉換須多次開檔的時間(控制項一定會有多個)
                int intIndex = 0;
                short intResouceFileQty = 2;
                if (intResouceFileQty == 1)
                {
                    oResourceDictionary = null;
                }
                else if (intResouceFileQty > 1)
                {
                    oResourceDictionary = new Dictionary<string, string>[intResouceFileQty - 2 + 1];
                }
                oResourceManager = ResourceManager.CreateFileBasedResourceManager(strResourceFile, pResourcePath, null);
                oResourceDictionary[0] = funReadResourceFile_Dictionary(pResourcePath + @"\" + strResourceFile + ".resources91");

                blnResult = true;
            }

            catch (Exception ex)
            {
                throw;
            }

            return blnResult;

        }


        /// <summary>
        /// 2020/05/15 OwenLiu, Mantis:0071895, 從所有語系檔案的 ResourceManager Array 取出對應的語系值
        /// </summary>
        /// <param name="pKey"></param>
        /// <param name="oResourceManager"></param>
        /// <returns></returns>
        private static string funGetResourceManager_Text(string pKey, ref ResourceManager oResourceManager, ref Dictionary<string, string>[] oResourceDictionary)
        {
            string strResult = "";
            bool blnKeyExists = false;

            do
            {
                try
                {

                    // 先搜尋Dictionary 是否有對應的Key
                    if (oResourceDictionary != null)
                    {
                        foreach (Dictionary<string, string> ResourceDic in oResourceDictionary)
                        {
                            if (ResourceDic.ContainsKey(pKey))
                            {
                                strResult = ResourceDic[pKey];
                                blnKeyExists = true;
                                break;
                            }
                        }
                    }

                    if (!blnKeyExists)
                    {
                        // 未找到Key, 搜尋ResourceManager
                        strResult = oResourceManager.GetString(pKey);
                        if (!string.IsNullOrEmpty(strResult))
                            break;
                    }
                }

                catch (Exception ex)
                {
                }
            }
            // Throw
            while (false);

            return strResult;

        }

        public static string TranslateMsg(string Message)
        {
            string TranslateMsgRet = default(string);

            // 1.Check LanguageMode
            string strFileName, strGenFile;
            string LanguageMode = AppSettings.Get("LanguageMode");
            if (string.IsNullOrEmpty(LanguageMode))
            {
                strFileName = "mesresource.en";
                strGenFile = "mesresource.en";
            }
            else
            {
                strFileName = "mesresource." + LanguageMode;
                strGenFile = "mesresource." + LanguageMode;
            }

            // 2.Get File Path
            string strResourcePath = "";
            string strResourceDirectory = AppSettings.Get("ResourceDir");

            // 2016/05/13 YF, 修改Server.MapPath取得實體路徑
            if (HttpContext.Current != null && HttpContext.Current.Server != null)
            {
                strResourcePath = HttpContext.Current.Server.MapPath("./" + strResourceDirectory);
            }

            string strErrName_Rep = "";

            // 檢查檔案是否存在
            if (File.Exists(strResourcePath + "/" + strFileName + ".resources") == true)
            {
                // 4.Replace原始訊息.
                if (Strings.InStr(Message, "[%", CompareMethod.Text) > 0)
                {
                    // Replace[%%]
                    // Dim rmGen As ResourceManager = ResourceManager.CreateFileBasedResourceManager(strGenFile, strResourcePath, Nothing)
                    // 2014/03/20 Hank, 修正讀取Resources檔造成佔用的情形
                    // Dim fs As New FileStream(strResourcePath.ToString & "/" & strFileName & ".resources", FileMode.Open, FileAccess.Read, FileShare.Read)
                    // Dim rmGen As New ResourceSet(fs)
                    // fs.Close()

                    // 2020/05/20 OwenLiu, Mantis:0071895, 增加多語系檔案的語系轉換機制
                    ResourceManager ResourceMgr = null;
                    Dictionary<string, string>[] ResourceDictionary = null;
                    funGetMultiResourceManager(strResourcePath, LanguageMode, ref ResourceMgr, ref ResourceDictionary);

                    string strKey, strKeyValue;
                    int intStart, j, k, l;

                    intStart = 1;
                    j = 1;

                    while (j != 0)
                    {
                        j = Strings.InStr(intStart, Message, "[%", CompareMethod.Text);
                        if (j == 0)
                        {
                            strErrName_Rep += Strings.Mid(Message, intStart);
                        }
                        else
                        {
                            k = Strings.InStr(j + 2, Message, "%]", CompareMethod.Text);
                            if (k == 0)
                            {
                                // 找不到對應的結束字元.
                                strErrName_Rep += Strings.Mid(Message, intStart);
                                j = 0;
                            }
                            else
                            {
                                // 檢查是否有不對稱的情況.
                                l = Strings.InStr(j + 2, Message, "[%", CompareMethod.Text);
                                if (l == 0 || l > k)
                                {
                                    strErrName_Rep += Strings.Mid(Message, intStart, j - intStart);
                                    strKey = Strings.Mid(Message, j + 2, k - j - 2);
                                    // 將Key執行語系轉換
                                    try
                                    {
                                        // 2020/05/20 OwenLiu, Mantis:0071895, 增加多語系檔案的語系轉換機制
                                        strKeyValue = funGetResourceManager_Text(strKey.ToUpper(), oResourceManager: ref ResourceMgr, oResourceDictionary: ref ResourceDictionary);
                                    }
                                    catch (Exception ex)
                                    {
                                        strKeyValue = strKey;
                                    }
                                    if (string.IsNullOrEmpty(strKeyValue))
                                    {
                                        strErrName_Rep += strKey;
                                    }
                                    else
                                    {
                                        strErrName_Rep += strKeyValue;
                                    }
                                    // 下次截取字串的起始位置
                                    intStart = k + 2;
                                }
                                else
                                {
                                    // 不對稱,找到最內層的[%%]
                                    int m;
                                    // 在前面的條件下,一定找得到.
                                    m = Strings.InStrRev(Message, "[%", k, CompareMethod.Text);
                                    // 擷出不轉換的字串.
                                    strErrName_Rep += Strings.Mid(Message, intStart, m - intStart);
                                    strKey = Strings.Mid(Message, m + 2, k - m - 2);
                                    // 將Key執行語系轉換
                                    try
                                    {
                                        // 2020/05/20 OwenLiu, Mantis:0071895, 增加多語系檔案的語系轉換機制
                                        strKeyValue = funGetResourceManager_Text(strKey.ToUpper(), oResourceManager: ref ResourceMgr, oResourceDictionary: ref ResourceDictionary);
                                    }
                                    catch (Exception ex)
                                    {
                                        strKeyValue = strKey;
                                    }
                                    if (string.IsNullOrEmpty(strKeyValue))
                                    {
                                        strErrName_Rep += strKey;
                                    }
                                    else
                                    {
                                        strErrName_Rep += strKeyValue;
                                    }
                                    // 下次截取字串的起始位置
                                    intStart = k + 2;
                                }
                            }
                        }
                    }

                    ResourceMgr = null;
                    ResourceDictionary = null;
                }
                else
                {
                    strErrName_Rep = Message;
                }
            }
            else
            {
                strErrName_Rep = Message.Replace("[%", "").Replace("%]", "");
            }

            TranslateMsgRet = strErrName_Rep;
            return TranslateMsgRet;

        }

        public static string TranslateMsg(string Message, string pLanguage)
        {
            string TranslateMsgRet = default(string);

            // 1.Check LanguageMode
            string strFileName, strGenFile;
            string LanguageMode = pLanguage;
            if (string.IsNullOrEmpty(LanguageMode))
            {
                strFileName = "mesresource.en";
                strGenFile = "mesresource.en";
            }
            else
            {
                strFileName = "mesresource." + LanguageMode;
                strGenFile = "mesresource." + LanguageMode;
            }

            // 2.Get File Path
            string strResourcePath = "";
            string strResourceDirectory = AppSettings.Get("ResourceDir");

            string strErrName_Rep = "";

            if (HttpContext.Current != null && HttpContext.Current.Server != null)
            {
                strResourcePath = HttpContext.Current.Server.MapPath("./" + strResourceDirectory);
            }

            // 檢查檔案是否存在
            if (File.Exists(strResourcePath + "/" + strFileName + ".resources") == true)
            {
                // 4.Replace原始訊息.
                if (Strings.InStr(Message, "[%", CompareMethod.Text) > 0)
                {
                    // Replace[%%]
                    // Dim rmGen As ResourceManager = ResourceManager.CreateFileBasedResourceManager(strGenFile, strResourcePath, Nothing)
                    // 2014/03/20 Hank, 修正讀取Resources檔造成佔用的情形
                    // Dim fs As New FileStream(strResourcePath.ToString & "/" & strFileName & ".resources", FileMode.Open, FileAccess.Read, FileShare.Read)
                    // Dim rmGen As New ResourceSet(fs)
                    // fs.Close()

                    // 2020/05/20 OwenLiu, Mantis:0071895, 增加多語系檔案的語系轉換機制
                    ResourceManager ResourceMgr = null;
                    Dictionary<string, string>[] ResourceDictionary = null;
                    funGetMultiResourceManager(strResourcePath, pLanguage, ref ResourceMgr, ref ResourceDictionary);

                    string strKey, strKeyValue;
                    int intStart, j, k, l;

                    intStart = 1;
                    j = 1;

                    while (j != 0)
                    {
                        j = Strings.InStr(intStart, Message, "[%", CompareMethod.Text);
                        if (j == 0)
                        {
                            strErrName_Rep += Strings.Mid(Message, intStart);
                        }
                        else
                        {
                            k = Strings.InStr(j + 2, Message, "%]", CompareMethod.Text);
                            if (k == 0)
                            {
                                // 找不到對應的結束字元.
                                strErrName_Rep += Strings.Mid(Message, intStart);
                                j = 0;
                            }
                            else
                            {
                                // 檢查是否有不對稱的情況.
                                l = Strings.InStr(j + 2, Message, "[%", CompareMethod.Text);
                                if (l == 0 || l > k)
                                {
                                    strErrName_Rep += Strings.Mid(Message, intStart, j - intStart);
                                    strKey = Strings.Mid(Message, j + 2, k - j - 2);
                                    // 將Key執行語系轉換
                                    try
                                    {
                                        // 2020/05/20 OwenLiu, Mantis:0071895, 增加多語系檔案的語系轉換機制
                                        strKeyValue = funGetResourceManager_Text(strKey.ToUpper(), oResourceManager: ref ResourceMgr, oResourceDictionary: ref ResourceDictionary);
                                    }
                                    catch (Exception ex)
                                    {
                                        strKeyValue = strKey;
                                    }
                                    if (string.IsNullOrEmpty(strKeyValue))
                                    {
                                        strErrName_Rep += strKey;
                                    }
                                    else
                                    {
                                        strErrName_Rep += strKeyValue;
                                    }
                                    // 下次截取字串的起始位置
                                    intStart = k + 2;
                                }
                                else
                                {
                                    // 不對稱,找到最內層的[%%]
                                    int m;
                                    // 在前面的條件下,一定找得到.
                                    m = Strings.InStrRev(Message, "[%", k, CompareMethod.Text);
                                    // 擷出不轉換的字串.
                                    strErrName_Rep += Strings.Mid(Message, intStart, m - intStart);
                                    strKey = Strings.Mid(Message, m + 2, k - m - 2);
                                    // 將Key執行語系轉換
                                    try
                                    {
                                        // 2020/05/20 OwenLiu, Mantis:0071895, 增加多語系檔案的語系轉換機制
                                        strKeyValue = funGetResourceManager_Text(strKey.ToUpper(), oResourceManager: ref ResourceMgr, oResourceDictionary: ref ResourceDictionary);
                                    }
                                    catch (Exception ex)
                                    {
                                        strKeyValue = strKey;
                                    }
                                    if (string.IsNullOrEmpty(strKeyValue))
                                    {
                                        strErrName_Rep += strKey;
                                    }
                                    else
                                    {
                                        strErrName_Rep += strKeyValue;
                                    }
                                    // 下次截取字串的起始位置
                                    intStart = k + 2;
                                }
                            }
                        }
                    }

                    ResourceMgr = null;
                    ResourceDictionary = null;
                }
                else
                {
                    strErrName_Rep = Message;
                }
            }
            else
            {
                strErrName_Rep = Message.Replace("[%", "").Replace("%]", "");
            }

            TranslateMsgRet = strErrName_Rep;
            return TranslateMsgRet;

        }

        #endregion

    }
}