解析郵件內容
#region 解析郵件內容
/// <summary>
/// 解析郵件內容
/// 創建人:yeqx
/// 創建時間:2011-05-20
/// </summary>
/// <param name="MsgContent">郵件內容</param>
/// <param name="Parameters">條件集</param>
/// <returns>解析后的郵件內容</returns>
/// <remarks>特殊字符用"<%TableName.FieldName%>"包圍</remarks>
public static string AnalysisEmailContent(string MsgContent, IDictionary<string, string> Parameters)
{
try
{
if (string.IsNullOrEmpty(MsgContent))
{
return MsgContent;
}
//使用foreach循環獲取
//Dictionary<string, string> DicField = GetField(MsgContent); //用來存放特殊字符
//使用正則表達式獲取
string RegexString = @"<%(?<text>[^%>]*)%>"; //用來存放特殊字符
Dictionary<string, string> DicField = GetRegValue(MsgContent, RegexString, "text", true);
if (DicField.Count == 0)
{
return MsgContent;
}
Dictionary<string, string> DicTemp = GetFiledValue(DicField, Parameters);//解析特殊字符值
string strTemp = string.Empty;
foreach (KeyValuePair<string, string> kvp in DicTemp)
{
strTemp = "<%" + kvp.Key + "%>";
MsgContent = MsgContent.Replace(strTemp, kvp.Value);//替換特殊字符為數據庫數據
}
return MsgContent;
}
catch (Exception exception)
{
JSUtil.Alert(Util.GetMessage("SystemErrInfo"));
Util.LogInfo(exception, "CommonUtil", "AnalysisEmailContent", true);
return string.Empty;
}
}
/// <summary>
/// 正则表达式取值
/// </summary>
/// <param name="HtmlCode">源码</param>
/// <param name="RegexString">正则表达式</param>
/// <param name="GroupKey">正则表达式分组关键字</param>
/// <param name="RightToLeft">是否从右到左</param>
/// <returns></returns>
private static Dictionary<string, string> GetRegValue(string HtmlCode, string RegexString, string GroupKey, bool RightToLeft)
{
bool blnFlag = true;
MatchCollection m;
Regex r;
if (RightToLeft == true)
{
r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
}
else
{
r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
}
m = r.Matches(HtmlCode);
Dictionary<string, string> DicField = new Dictionary<string, string>(); //用來存放特殊字符
string strTemp = string.Empty;
System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[A-Za-z0-9._]+$");
foreach (Match match in m)
{
strTemp = match.Groups[GroupKey].Value;
blnFlag = !DicField.ContainsKey(strTemp) && reg1.IsMatch(strTemp);
if (blnFlag)
{
DicField.Add(strTemp, string.Empty);
}
}
return DicField;
}
/// <summary>
/// 根據特殊集合獲取值
/// 創建人:yeqx
/// 創建時間:2011-05-23
/// </summary>
/// <param name="GetField">特殊字符集</param>
/// <param name="DicField">參數集</param>
/// <returns>特殊字符對應值</returns>
private static Dictionary<string, string> GetFiledValue(Dictionary<string, string> DicField, IDictionary<string, string> Parameters)
{
try
{
bool blnFlag = true;
//對需要解析的文字讀取數據庫
Hashtable htParameters = new Hashtable();
DataSet dstResult = new DataSet();
Dictionary<string, string> DicTemp = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> kvp in DicField)
{
htParameters.Clear();
dstResult = new DataSet();
htParameters["pTableName"] = kvp.Key.Split('.')[0];
htParameters["pFieldName"] = kvp.Key.Split('.')[1];
//判斷參數是否包含此字段的
blnFlag = Parameters.Keys.Contains(kvp.Key);
if (blnFlag)
{
htParameters["pConditions"] = Parameters[kvp.Key];
}
else
{
htParameters["pConditions"] = string.Empty;
}
dstResult = QueryHandle.QueryForDataSet("usp_GetFiledValue", htParameters);
blnFlag = dstResult != null && dstResult.Tables.Count > 0 && dstResult.Tables[0].Rows.Count > 0;
DicTemp.Add(kvp.Key, string.Empty);
if (blnFlag)
{
foreach (DataRow drowItem in dstResult.Tables[0].Rows)
{
DicTemp[kvp.Key] += drowItem[0].ToString() + ",";
}
if (DicTemp[kvp.Key].Length > 0)
{
DicTemp[kvp.Key] = DicTemp[kvp.Key].Substring(0, DicTemp[kvp.Key].Length - 1);
}
}
}
return DicTemp;
}
catch (Exception exception)
{
JSUtil.Alert(Util.GetMessage("SystemErrInfo"));
Util.LogInfo(exception, "CommonUtil", "AnalysisEmailContent", true);
return new Dictionary<string, string>();
}
}
#endregion
/// <summary>
/// 解析郵件內容
/// 創建人:yeqx
/// 創建時間:2011-05-20
/// </summary>
/// <param name="MsgContent">郵件內容</param>
/// <param name="Parameters">條件集</param>
/// <returns>解析后的郵件內容</returns>
/// <remarks>特殊字符用"<%TableName.FieldName%>"包圍</remarks>
public static string AnalysisEmailContent(string MsgContent, IDictionary<string, string> Parameters)
{
try
{
if (string.IsNullOrEmpty(MsgContent))
{
return MsgContent;
}
//使用foreach循環獲取
//Dictionary<string, string> DicField = GetField(MsgContent); //用來存放特殊字符
//使用正則表達式獲取
string RegexString = @"<%(?<text>[^%>]*)%>"; //用來存放特殊字符
Dictionary<string, string> DicField = GetRegValue(MsgContent, RegexString, "text", true);
if (DicField.Count == 0)
{
return MsgContent;
}
Dictionary<string, string> DicTemp = GetFiledValue(DicField, Parameters);//解析特殊字符值
string strTemp = string.Empty;
foreach (KeyValuePair<string, string> kvp in DicTemp)
{
strTemp = "<%" + kvp.Key + "%>";
MsgContent = MsgContent.Replace(strTemp, kvp.Value);//替換特殊字符為數據庫數據
}
return MsgContent;
}
catch (Exception exception)
{
JSUtil.Alert(Util.GetMessage("SystemErrInfo"));
Util.LogInfo(exception, "CommonUtil", "AnalysisEmailContent", true);
return string.Empty;
}
}
/// <summary>
/// 正则表达式取值
/// </summary>
/// <param name="HtmlCode">源码</param>
/// <param name="RegexString">正则表达式</param>
/// <param name="GroupKey">正则表达式分组关键字</param>
/// <param name="RightToLeft">是否从右到左</param>
/// <returns></returns>
private static Dictionary<string, string> GetRegValue(string HtmlCode, string RegexString, string GroupKey, bool RightToLeft)
{
bool blnFlag = true;
MatchCollection m;
Regex r;
if (RightToLeft == true)
{
r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
}
else
{
r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
}
m = r.Matches(HtmlCode);
Dictionary<string, string> DicField = new Dictionary<string, string>(); //用來存放特殊字符
string strTemp = string.Empty;
System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[A-Za-z0-9._]+$");
foreach (Match match in m)
{
strTemp = match.Groups[GroupKey].Value;
blnFlag = !DicField.ContainsKey(strTemp) && reg1.IsMatch(strTemp);
if (blnFlag)
{
DicField.Add(strTemp, string.Empty);
}
}
return DicField;
}
/// <summary>
/// 根據特殊集合獲取值
/// 創建人:yeqx
/// 創建時間:2011-05-23
/// </summary>
/// <param name="GetField">特殊字符集</param>
/// <param name="DicField">參數集</param>
/// <returns>特殊字符對應值</returns>
private static Dictionary<string, string> GetFiledValue(Dictionary<string, string> DicField, IDictionary<string, string> Parameters)
{
try
{
bool blnFlag = true;
//對需要解析的文字讀取數據庫
Hashtable htParameters = new Hashtable();
DataSet dstResult = new DataSet();
Dictionary<string, string> DicTemp = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> kvp in DicField)
{
htParameters.Clear();
dstResult = new DataSet();
htParameters["pTableName"] = kvp.Key.Split('.')[0];
htParameters["pFieldName"] = kvp.Key.Split('.')[1];
//判斷參數是否包含此字段的
blnFlag = Parameters.Keys.Contains(kvp.Key);
if (blnFlag)
{
htParameters["pConditions"] = Parameters[kvp.Key];
}
else
{
htParameters["pConditions"] = string.Empty;
}
dstResult = QueryHandle.QueryForDataSet("usp_GetFiledValue", htParameters);
blnFlag = dstResult != null && dstResult.Tables.Count > 0 && dstResult.Tables[0].Rows.Count > 0;
DicTemp.Add(kvp.Key, string.Empty);
if (blnFlag)
{
foreach (DataRow drowItem in dstResult.Tables[0].Rows)
{
DicTemp[kvp.Key] += drowItem[0].ToString() + ",";
}
if (DicTemp[kvp.Key].Length > 0)
{
DicTemp[kvp.Key] = DicTemp[kvp.Key].Substring(0, DicTemp[kvp.Key].Length - 1);
}
}
}
return DicTemp;
}
catch (Exception exception)
{
JSUtil.Alert(Util.GetMessage("SystemErrInfo"));
Util.LogInfo(exception, "CommonUtil", "AnalysisEmailContent", true);
return new Dictionary<string, string>();
}
}
#endregion