Create a static page generation example:
Generate a static page by replacing the template page
Step 1: Create a project and create a simple template page: templatepage.htm
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> simple example of generating static pages by porschev </title>
</Head>
<Body>
<H1> $ porschev [0] $ <Ul>
<Li> page title: $ porschev [0] $ </LI>
<Li> name: $ porschev [1] $ </LI>
<Li> URL: <a href = "$ porschev [2] $" target = "_ blank"> $ porschev [2] $ </a> </LI>
<Li> time: $ porschev [3] $ </LI>
<Li> details: $ porschev [4] $ </LI>
</Ul>
</Body>
</Html>
Step 2: Create a Config File: createhtml. config
<?xml version="1.0" encoding="utf-8" ?>
<web>
<website key="0" value="title"/>
<website key="1" value="name"/>
<website key="2" value="url"/>
<website key="3" value="createDate"/>
<website key="4" value="desc"/>
</web>
Step 3: Compile static page generation Code: (add system. Web reference)
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. IO;
Using system. xml;
Namespace createhtmlbll
{
Public class createhtmlbll
{
# Region # number of nodes read from the configuration file
/// <Summary>
/// Read the number of nodes in the configuration file
/// </Summary>
/// <Param name = "path"> path of the configuration file </param>
/// <Param name = "nodename"> node to be retrieved </param>
/// <Returns> Number of returned nodes </returns>
Private int readconfig (string path, string nodename)
{
String absopath = string. Empty; // absolute path
Try
{
Absopath = system. Web. httpcontext. Current. server. mappath (PATH );
Xmldocument XD = new xmldocument ();
XD. Load (absopath );
Xmlnodelist nodelist = XD. selectnodes (nodename); // get the set of corresponding nodes
Return nodelist. count;
}
Catch (exception)
{
Throw;
}
}
# Endregion
# Region # create a folder
/// <Summary>
/// Create a folder
/// </Summary>
/// <Param name = "path"> path to be created </param>
Public void creatfolder (string path)
{
String absopath = string. Empty; // absolute path
Try
{
Absopath = system. Web. httpcontext. Current. server. mappath (PATH );
If (! Directory. exists (absopath ))
{
Directory. createdirectory (absopath );
}
}
Catch (exception)
{
Throw;
}
}
# Endregion
# Region # generate an HTML page
/// <Summary>
/// Generate an HTML page
/// </Summary>
/// <Param name = "configpath"> path of the configuration file </param>
/// <Param name = "confignodename"> Configuration File node name </param>
/// <Param name = "tempath"> template page path </param>
/// <Param name = "arr"> replace an array </param>
/// <Param name = "createpath"> Generate an HTML path </param>
Public void createhtml (string configpath, string confignodename, string tempath, string [] arr, string createpath)
{
String filename = string. Empty; // generate a file name
String absocrepath = string. Empty; // the absolute path of the generated page.
String absotempath = string. Empty; // absolute path of the template page
Int nodecount = 0; // number of nodes
Try
{
Absocrepath = system. Web. httpcontext. Current. server. mappath (createpath );
Absotempath = system. Web. httpcontext. Current. server. mappath (tempath );
Nodecount = readconfig (configpath, confignodename );
Filestream FS = file. Open (absotempath, filemode. Open, fileaccess. Read); // read the template page
Streamreader sr = new streamreader (FS, encoding. getencoding ("UTF-8 "));
Stringbuilder sb = new stringbuilder (Sr. readtoend ());
Sr. Close ();
Sr. Dispose ();
For (INT I = 0; I <nodecount; I ++)
{
SB. Replace ("$ porschev [" + I + "] $", arr [I]);
}
Creatfolder (createpath );
Filename = datetime. Now. tofiletime (). tostring () + ". html"; // set the file name (you can change the name as needed)
Filestream CFS = file. Create (absocrepath + "/" + filename );
Streamwriter Sw = new streamwriter (CFS, encoding. getencoding ("UTF-8 "));
Sw. Write (sb. tostring ());
Sw. Flush ();
Sw. Close ();
Sw. Dispose ();
}
Catch (exception)
{
Throw;
}
}
# Endregion
}
}
Step 4: Test Generation
Protected void page_load (Object sender, eventargs E)
{
Createhtml ();
}
# Region # generate a static page
/// <Summary>
/// Generate a static page
/// </Summary>
Public void createhtml ()
{
Try
{
String [] arr = new string [5];
Arr [0] = "porschev Static Page Test Type ";
Arr [1] = "dtan ";
Arr [2] = "www. dtan. So ";
Arr [3] = datetime. Today. tostring ();
Arr [4] = "csdn during working hours is not a good job... ";
Createhtmlbll. createhtmlbll CHB = new createhtmlbll. createhtmlbll ();
CHB. createhtml ("createhtml. config", "Web/Website", "template/templatepage.htm", arr, "porschev ");
}
Catch (exception ex)
{
Throw ex;
}
}
# Endregion
Generate page:
Sample source code download: http://download.csdn.net/source/3293015
Sample source code download 2: http://files.cnblogs.com/zhongweiv/CreateHtml.rar