根据html页面模板动态生成html页面(c#类)_c#应…

2008-02-23 05:43:33来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

一直以为动态生成静态页面不好做,昨天在网上找了下,我晕,其实很简单,思路大概是这样的,
1:建立一个html页面模板,在这个页面中把您想要动态显示的地方用特别的字符串表示(如$htmlstrstr$);
2:在程式中用将这个html页面读到一个字符串变量如str;
3:用字符串的resplace方法将在第一步中特别字符替换成您想要的内容;
4保存;
OK,so easy,今天就用C#写了一个这样的类,用来处理动态生成html页面的,自认为还写的完整,刚接触.NET不久,望指教,完整代码和示例在此下载:下载http://www.cnblogs.com/Files/solucky/aspxTohtml.rar

转贴请注明出处,谢谢!
注:此类中的代码不全是原创,部份代码参照网友的代码!

以下是转换类的代码


代码
1using System;
2using System.Text;
3using System.Web;
4using System.Configuration;
5using System.IO;
6namespace solucky
7{
8 /**//// <summary>
9 /// AspxToHtml 的摘要说明。
10 /// 注:使用此类,您能够在web.config文档对模板类进行配置.如下
11 /**//*<appSettings>
12 <add key="templateFilePath" value="htmlmoudel.htm" />
13 <add key="htmlFilePath" value="new/"></add>
14 <add key="ErrLogPath" value="aspxTohtml_log.txt"></add>
15 </appSettings>*/
16 /**//// </summary>
17 public class AspxToHtml
18 {
19 /**//// <summary>
20 /// 模板文档中要替代的参数个数
21 /// </summary>
22 private int _templateParamCount=0;
23 /**//// <summary>
24 /// 模板文档所在的路径
25 /// </summary>
26 private string _templateFilePath =ConfigurationSettings.AppSettings["templateFilePath"];
27 /**//// <summary>
28 /// 转换后的html文档所存放的路径
29 /// </summary>
30 private string _htmlFilePath =ConfigurationSettings.AppSettings["htmlFilePath"];
31
32 /**//// <summary>
33 /// 模板页页面编码
34 /// </summary>
35 private Encoding _templateHtmlCode =Encoding.GetEncoding("gb2312");
36
37 /**//// <summary>
38 /// 转换后的文档编码
39 /// </summary>
40 private Encoding _code = Encoding.GetEncoding("gb2312");
41
42 /**//// <summary>
43 /// 转换后的html文档名
44 /// </summary>
45 private string _convertedFilename="";
46 /**//// <summary>
47 /// 模板文档中的参数
48 /// </summary>
49 private string[] _templateFileparameter ;
50
51 /**//// <summary>
52 /// aspx文档中的要代替HTML文档中的参数实际值
53 /// </summary>
54 private string[] _aspxFileparameter;
55
56 private string _errlogPath = ConfigurationSettings.AppSettings["ErrLogPath"];
57
58 属性#region 属性
59
60 /**//// <summary>
61 /// 模板文档中要替代的参数个数
62 /// </summary>
63 public int TemplateParamCount
64 {
65 get
66 {
67 return this._templateParamCount;
68 }
69 set//分配参数个数时,同时为模板文档中的参数和aspx文档中的要代替HTML文档中的参数实际值这两个分配实际数组
70 {
71 if (value < 0)
72 throw new ArgumentException();
73
74 if(value>0)
75 {
76 this._templateParamCount=value;
77 //模板文档中的参数
78 _templateFileparameter = new string[value];
79 //aspx文档中的要代替HTML文档中的参数实际值
80 _aspxFileparameter = new string[value];
81 }
82 else
83 this._templateParamCount=0;
84 }
85 }
86
87 /**//// <summary>
88 /// 模板文档所在的路径
89 ///
90 /// </summary>
91 public string TemplateFilePath
92 {
93 get{ return this._templateFilePath;}
94 set{ this._templateFilePath=value;}
95 }
96 /**//// <summary>
97 /// 转换后的html文档所存放的路径
98 /// </summary>
99 public string HtmlFilePath
100 {
101 get{ return this._htmlFilePath;}
102 set{ this._htmlFilePath=value;}
103 }
104
105 /**//// <summary>
106 /// html模板文档编码
107 /// </summary>
108 public Encoding TemplateHtmlCode
109 {
110 get{ return this._templateHtmlCode;}
111 set{ this._templateHtmlCode=Encoding.GetEncoding(value.ToString());}
112 }
113 /**//// <summary>

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: 再现c#导出excel源码_c#应用

下一篇: c#中实现随机时间的获取_c#应用