Transfer (http://www.soaspx.com/dotnet/csharp/csharp_20090902_265.html)
This function is applicable to websites with poor background database functions, that is, most of the text is not stored in the database records, but in html or xml files, and only the index is placed in the database, such as the title, category, and keyword of an article. This method is suitable for websites without database support such as MS SQL Server in the background.
Applicable to news publishing systems, such as sina and 163, which dynamically generate html pages.
Applicable to programs that require dynamic page customization. Such as forums and chat rooms. You can load custom html pages to enhance the appearance.
Ideas
1. use a tool such as Dw-Mx to generate an html template, and add special tags (such as $ htmlformat $) where the format needs to be added. When a file is dynamically generated, use the code to read the template, obtain the content entered at the front end, add it to the tag location of this template, generate a new file name, and write it to the disk. Then, write the data to the database.
2. Html files are hardcoded Using Background code. You can use the HtmlTextWriter class to write html files.
Advantages
1. you can create complex pages and add document to the js file by using methods that contain js files. the write () method can be used to add content such as the page header and advertisement to all pages.
2. The Index Server of MS Windows2000 can be used to create a full-text search engine for static html files, and asp.net can be used to obtain search results in DataTable mode. The Index Service of Win2000 cannot find the content of xml files. If both database search and Index search are included, this search function is very powerful.
3. Saving server load and saving a lot of resources by requesting a static html file than An aspx file server.
Disadvantages
Train of Thought 2: If hard encoding is used, the workload is very heavy and a lot of html code is required. Debugging is difficult. In addition, html styles generated by hard encoding cannot be modified. If the style of a website is changed, it must be re-encoded, which brings a huge workload in the future.
Therefore, the first approach is adopted here.
Column code
1.define (template.htm) html template page
<Html>
<Head>
<Title> </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
</Head>
<Body>
<Table $ htmlformat [0] 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: $ htmlformat [1]; font-size: $ htmlformat [2]"> $ htmlformat [3] </span>
</Td>
</Tr>
</Table>
</Body>
</Html>
2.asp.net code:
Public void WriteFile ()
{
// --------------------- Read the html template page to the stringbuilder object ----
String [] format = new string [4]; // defines an array with the same number of htmlyem tags
StringBuilder htmltext = new StringBuilder ();
Try
{
Using (StreamReader sr = new StreamReader (Server. MapPath (".") + "/Test/HTMLPage2.htm") // template page path
{
String line;
While (line = sr. ReadLine ())! = Null)
{
Htmltext. Append (line );
}
Sr. Close ();
}
}
Catch
{
Response. Write ("<Script> alert ('file reading error') </Script> ");
}
// --------------------- Assign a value to the tag array ------------
Format [0] = "background = \" bg.jpg \ ""; // background image
Format [1] = "#990099"; // font color
Format [2] = "150px"; // font size
Format [3] = "<marquee> generated template html page </marquee>"; // text description
// ---------- Replace the content marked in htm with the content you want to add
For (int I = 0; I <4; I ++)
{
Htmltext. Replace ("$ htmlformat [" + I + "]", format [I]);
}
// ---------- Generate the htm file ------------------――
Try
{
Using (StreamWriter sw = new StreamWriter (Server. mapPath (". ") +"/Test/test.htm ", false, System. text. encoding. getEncoding ("GB2312") // Save the address
{
Sw. WriteLine (htmltext );
Sw. Flush ();
Sw. Close ();
}
}
Catch
{
Response. Write ("The file cocould not be wirte :");
}
}
Summary
This method can be used to easily generate html files. The program uses cyclic replacement, so it is very fast for templates that need to replace a large number of elements.