| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 564 人关注过本帖
标题:。net动态生成静态实例2
只看楼主 加入收藏
work001
Rank: 3Rank: 3
等 级:禁止访问
帖 子:76
专家分:114
注 册:2010-4-9
收藏
 问题点数:0 回复次数:0 
。net动态生成静态实例2
第二分参考资料


实现动态创建HTML文件的功能需要应用StreamReader和StreamWriter类。

中,String对象是不可改变的,每次使用该类中的方法时,都要在内存中创建一个新的字符串对象,这就需要为新对象分配新的空间。在需要对字符串执行重复修改的情况下,就会增加系统开销。如果要修改字符串而不创建新的对象,则可以使用Sysmte.text命名空间下的StringBuilder类,该类可以改变字符串,当在一个循环中将许多字符串连接到一起时,使用StringBuildr类可以提高性能。

实现方法:首先使用StreamReader对象读取指定HTML模板文件内容,并把读取到的内容保存到StringBuilder对象中,然后通过for循环语句依次替换关键字段,最后再通过StreamWriter对象根据替换后新内容创建一个HTML文件。

代码如下:

页面文件:

新闻模板文件(template.html):

<html>
    <head>
        <title>$htmlkey[0]</title>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body >
        <table $htmlkey[1] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000">
          <tr>
            <td width="100%" valign="middle" align="left">
    <span style="color: $htmlkey[2];font-size: $htmlkey[3]"><marquee>$htmlkey[4]</marquee></span>
  </td>
          </tr>
        </table>
    </body>
</html>


页面文件:Dhtml.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dhtml.aspx.cs" Inherits="Dhtml" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.

<html xmlns="http://www. >
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="Form1" method="post" runat="server">
  <asp:Button id="btnCreate" style="Z-INDEX: 101; LEFT: 576px; POSITION: absolute; TOP: 48px"
  runat="server" Text="创建HTML文件" OnClick="btnCreate_Click"></asp:Button>
  <asp:TextBox id="txtContent" style="Z-INDEX: 102; LEFT: 208px; POSITION: absolute; TOP: 80px"
  runat="server" TextMode="MultiLine" Height="402px" Width="352px"></asp:TextBox>
  <asp:HyperLink id="hyCreateFile" style="Z-INDEX: 103; LEFT: 584px; POSITION: absolute; TOP: 96px"
  runat="server" Target="_blank">创建的HTML文件</asp:HyperLink>
  <asp:TextBox id="txtTitle" style="Z-INDEX: 104; LEFT: 208px; POSITION: absolute; TOP: 48px" runat="server"
  Width="352px"></asp:TextBox>
  <asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 128px; POSITION: absolute; TOP: 48px" runat="server">页面标题</asp:Label>
  <asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 128px; POSITION: absolute; TOP: 80px" runat="server">页面内容</asp:Label>
</form>
</body>
</html>


实现代码:Dhtml.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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

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

  }
  protected void btnCreate_Click(object sender, EventArgs e)
  {
    string[] newContent = new string[5];//定义和html标记数目一致的数组
    StringBuilder strhtml = new StringBuilder();
    try
    {
        //创建StreamReader对象
        using (StreamReader sr = new StreamReader(Server.MapPath("TextBox") + "\\template.html"))
        {
          string oneline;
          //读取指定的HTML文件模板
          while ((oneline = sr.ReadLine()) != null)
          {
            strhtml.Append(oneline);
          }
          sr.Close();
        }
    }
    catch (Exception err)
    {
        //输出异常信息
        Response.Write(err.ToString());
    }
    //为标记数组赋值
    newContent[0] = txtTitle.Text;//标题
    newContent[1] = "BackColor='#cccfff'";//背景色
    newContent[2] = "#ff0000";//字体颜色
    newContent[3] = "100px";//字体大小
    newContent[4] = txtContent.Text;//主要内容

    //根据上面新的内容生成html文件
    try
    {
        //指定要生成的HTML文件
        string fname = Server.MapPath("TextBox") + "\\" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";
        //替换html模版文件里的标记为新的内容
        for (int i = 0; i < 5; i++)
        {
          strhtml.Replace("$htmlkey[" + i + "]", newContent);
        }
        //创建文件信息对象
        FileInfo finfo = new FileInfo(fname);
        //以打开或者写入的形式创建文件流
        using (FileStream fs = finfo.OpenWrite())
        {
          //根据上面创建的文件流创建写数据流
          StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
          //把新的内容写到创建的HTML页面中
          sw.WriteLine(strhtml);
          sw.Flush();
          sw.Close();
        }
        //设置超级链接的属性
        hyCreateFile.Text = DateTime.Now.ToString("yyyymmddhhmmss") + ".html";
        hyCreateFile.NavigateUrl = "TextBox/" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";
    }
    catch (Exception err)
    {
        Response.Write(err.ToString());
    }
  }
}


搜索更多相关主题的帖子: 实例 动态 静态 
2010-04-09 10:11
快速回复:。net动态生成静态实例2
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012620 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved