C#对html的操作

1,获取相对路径的html然后保存到本地路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/// <summary>
/// Html方法
/// </summary>
/// <returns></returns>
public void CollectionContent(string url)
{
    Response.ContentType = "html/plain";
    Response.Charset = "gb2312";
    Response.ContentType = "html/html";
    Response.Charset = "utf-8";
    string htmlContent = clsCommonEx.GetPageHtml(url);
 
    try
    {
 
        StreamWriter sw = null;
        Encoding code = Encoding.GetEncoding("gb2312");
        string path = HttpContext.Current.Server.MapPath("/cache/");
        string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";// 写文件
 
        try
        {
            sw = new StreamWriter(path + htmlfilename, false, code);
            sw.Write(htmlContent);
            sw.Flush();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
        }
 
    }
    catch
    {
        Response.Write("The file could not be wirte:");
    }
 
}

  

2,生成html

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//生成HTML页
public static bool WriteFile(string strText, string strContent, string strAuthor)
{
    string path = HttpContext.Current.Server.MapPath("/news/");
    Encoding code = Encoding.GetEncoding("gb2312");
    // 读取模板文件
    string temp = HttpContext.Current.Server.MapPath("/news/text.html");
    StreamReader sr = null;
    StreamWriter sw = null;
    string str = "";
    try
    {
        sr = new StreamReader(temp, code);
        str = sr.ReadToEnd(); // 读取文件
    }
    catch (Exception exp)
    {
        HttpContext.Current.Response.Write(exp.Message);
        HttpContext.Current.Response.End();
        sr.Close();
    }
 
 
    string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
    // 替换内容
    // 这时,模板文件已经读入到名称为str的变量中了
    str = str.Replace("ShowArticle", strText); //模板页中的ShowArticle
    str = str.Replace("biaoti", strText);
    str = str.Replace("content", strContent);
    str = str.Replace("author", strAuthor);
    // 写文件
    try
    {
        sw = new StreamWriter(path + htmlfilename, false, code);
        sw.Write(str);
        sw.Flush();
    }
    catch (Exception ex)
    {
        HttpContext.Current.Response.Write(ex.Message);
        HttpContext.Current.Response.End();
    }
    finally
    {
        sw.Close();
    }
    return true;
}

  

posted @   逊老头  阅读(2300)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· ASP.NET Core - 日志记录系统(二)
阅读排行:
· 支付宝事故这事儿,凭什么又是程序员背锅?有没有可能是这样的...
· 在线客服系统 QPS 突破 240/秒,连接数突破 4000,日请求数接近1000万次,.NET 多
· C# 开发工具Visual Studio 介绍
· 在 Windows 10 上实现免密码 SSH 登录
· C#中如何使用异步编程
点击右上角即可分享
微信分享提示