asp.net 生成html 简单示例

模板文件内容:
mb.html

<html>
<head>
    
<title>Create Html</title>
</head>
<body>
    
<form id="form1">
    
<div>
       
<table>
<tr>
<td>
<br><br>
title:$title$
<br><br>
content:$content$
<br><br>
</td>
</tr>
</table>
    
</div>
    
</form>
</body>
</html>

代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
string mbPath = "c:\\mb.html";
        Encoding code 
= Encoding.GetEncoding("gb2312");
        StreamReader sr 
= null;
        StreamWriter sw 
= null;
        
string str = null;

        
//读取
        try
        
{
            sr 
= new StreamReader(mbPath, code);
            str 
= sr.ReadToEnd();

        }

        
catch (Exception ex)
        
{
            
throw ex;
        }

        
finally
        
{
            sr.Close();
        }


        
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss"+ ".shtml";
        str 
= str.Replace("$title$""mytitle");
        str 
= str.Replace("$content$""myContent");

        
//写入
        try
        
{
            sw 
= new StreamWriter("c:\\" + fileName, false, code);
            sw.Write(str);
            sw.Flush();

        }

        
catch (Exception ex)
        
{
            
throw ex;
        }

        
finally
        
{
            sw.Close();
        }


    }

}


生成后的文件内容:
20070515010734.shtml
<html>
<head>
    
<title>Create Html</title>
</head>
<body>
    
<form id="form1">
    
<div>
       
<table>
<tr>
<td>
<br><br>
title:mytitle
<br><br>
content:myContent
<br><br>
</td>
</tr>
</table>
    
</div>
    
</form>
</body>
</html>
posted @ 2007-05-15 01:12  yongwnet  阅读(322)  评论(0编辑  收藏  举报