Step 1: Create a project and create a simple template page: TemplatePage.htm
| The code is as follows: |
Copy code |
|
<! 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
| The code is as follows: |
Copy code |
|
<? 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)
| The code is as follows: |
Copy code |
|
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
| The code is as follows: |
Copy code |
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.111cn.net ";
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 |