Example #1
0
        /// <summary>
        /// 根据模板将JSON字符串填充到模板中
        /// </summary>
        /// <param name="modelName"></param>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public static string ToModelStringStatic(string modelName, string jsonStr)
        {
            string htmlText  = MapHtmlCache.GetMap(modelName);
            int    jsonIndex = htmlText.IndexOf("[[[]]]");

            if (jsonIndex == -1)
            {
                throw new ArgumentOutOfRangeException(string.Format("未能在HTML模板中找到JOSN数据占位符 {0}", modelName));
            }

            //string jsonStr = ToJsonStringByFastJson();

            StringBuilder sb = new StringBuilder(htmlText.Length + jsonStr.Length);

            sb.Append(htmlText.ToCharArray(0, jsonIndex));
            sb.Append(jsonStr);
            sb.Append(htmlText.ToCharArray(jsonIndex + 6, htmlText.Length - (jsonIndex + 6)));

            return(sb.ToString());
        }
Example #2
0
        /// <summary>
        /// 转为模板名称
        /// </summary>
        /// <param name="modelName"></param>
        /// <returns></returns>
        public string ToModelString(string modelName)
        {
            //string filePath = System.Configuration.ConfigurationManager.AppSettings[modelName];
            //string htmlText = System.IO.File.ReadAllText(
            //    string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, filePath.TrimStart('\\')));
            string htmlText  = MapHtmlCache.GetMap(modelName);
            int    jsonIndex = htmlText.IndexOf("[[[]]]");

            if (jsonIndex == -1)
            {
                throw new ArgumentOutOfRangeException(string.Format("未能在HTML模板中找到JOSN数据占位符 {0}", modelName));
            }

            string jsonStr = ToJsonStringByFastJson();

            StringBuilder sb = new StringBuilder(htmlText.Length + jsonStr.Length);

            sb.Append(htmlText.ToCharArray(0, jsonIndex));
            sb.Append(jsonStr);
            sb.Append(htmlText.ToCharArray(jsonIndex + 6, htmlText.Length - (jsonIndex + 6)));

            return(sb.ToString());
        }