自动生成页面

读写文件的帮助类

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Net;

/// <summary>
///HtmlDB 的摘要说明
/// </summary>
public class HtmlDB
{
    public HtmlDB()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public static bool WriteFile_1(string news_id, string news_ClassPath, string context)
    {
        //申明字符编码,命名空间System.Text
        Encoding encoding = Encoding.GetEncoding("utf-8");

        StreamWriter sw = null;
        string myPath = news_ClassPath;
        string OutPutPath = HttpContext.Current.Server.MapPath(myPath);
        string newName = news_id + ".html";
        string path = OutPutPath + newName;
        try
        {
            sw = new StreamWriter(OutPutPath + newName, false, encoding);
            context = context.Replace("&lt;", "<").Replace("&gt;", ">");
            sw.Write(context);
            sw.Flush();
        }
        catch (Exception e2)
        {
            HttpContext.Current.Response.Write(e2.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
            sw.Dispose();
        }
        return true;
    }
    //public static string GetInterIDList(string strfile)
    //{
    //    Encoding encoding = Encoding.GetEncoding("utf-8");
    //    string strout;
    //    strout = "";
    //    StreamReader sr = new StreamReader( strfile, System.Text.Encoding.UTF8);
    //    String input = sr.ReadToEnd();
    //    sr.Close();
    //    strout = input;
    //    return strout.Trim();
    //}
    public static string GetInterIDList(string strfile)
    {
        WebRequest req = WebRequest.Create(strfile);
        WebResponse result = req.GetResponse();
        //得到的流是网页内容   
        Stream ReceiveStream = result.GetResponseStream();
        try
        {
            StreamReader readerOfStream = new StreamReader(ReceiveStream, System.Text.Encoding.GetEncoding("UTF-8"));
            string CT_Voucher_Str = readerOfStream.ReadToEnd();
            return CT_Voucher_Str;
        }
        catch (Exception ex)
        {
            return "生成失败";
        }
        finally
        {
            //流关闭
            ReceiveStream.Close();
        }
    }
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ntours.MDAL.Group;
using Ntours.Factory.Group;
using System.Collections.Generic;
public partial class Manager_FIT_Air_Route_NewPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string id = Request["id"].ToString();
        FIT_Air_Route route = BLLFactoryClass.GetFIT_Air_RouteBLL().SelectFIT_Air_RouteById(id);
        List<FIT_Start_Date> staList = BLLFactoryClass.GetFIT_Start_DateBLL().SelectByAirRoute_ID(id);
        //拼接路径
        string file = "http://travel.ntours.cn/Default.aspx?id=" + id;
        //根据路径读取文件
        string sb = HtmlDB.GetInterIDList(file);
        string title = this.title.Value.ToString();
        string search = this.search.Value.ToString();
        string con = this.con.Value.ToString();
        //替换文件中的内容
        if (title != "" && search != "" && con != "")
        {
            sb = sb.Replace("无标题页", title);
            sb = sb.Replace("网站描述", search);
            sb = sb.Replace("关键词", con);
            int index = sb.IndexOf("%>") + 2;
            sb = sb.Replace(sb.Substring(0, index), "");
            sb = sb.Replace("<script src='JS/DetialJScript.js' type='text/javascript'></script>", "<script src='/JS/NewPaneJScript.js' type='text/javascript'></script>");
            //写入文件
            bool result = HtmlDB.WriteFile_1(id, "~/html/", sb);
            if (result)
            {
                Response.Write("<script>alert('生成成功')</script>");
                Response.Write("<script>location.href='FIT_Air_RouteList.aspx'</script>");
            }
            else
            {
                Response.Write("<script>alert('对不起生成失败,该航线没有出发日期')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('请输入标题关键字网站描述后再次生成')</script>");
        }
    }
}

 

posted @ 2014-04-21 16:57  流浪的狸猫  阅读(482)  评论(0编辑  收藏  举报