Website map creation based on C #

Source: Internet
Author: User

1. Our website is developed in C #, and we store all the article information in a database. Therefore, our articles are dynamically extracted from the database. This is not good for the spider.
2. Well, hey, it's barely possible. Creating a website Map allows users to view the content of a website at a glance and can play a very good role in navigation.
For the above reasons, I decided to create a website map for my local "Chitu English Network. Because the website maps required by Google and other search engines must be XML files, I decided to create two types of website maps: webpage form and XML form. Our website is developed in C #, and of course we use C # To compile the map of our website.
First, let's take a look at how our website map is generated and displayed. After a simple thought, I decided to do this. "Website map" is added at the bottom of the home page, which is linked to a website map in XML format. The page name is sitemap. xml. The two page generating programs are added in the background. Set a button. The Administrator clicks this button within a period of time to directly generate the two files and place them in the root directory of the website for the client to call at any time.
Next, let's take a look at the specific implementation of the Code for generating the website map. For readability, we will introduce the code implementation process from top to bottom.
I. Top-level design of website maps
The code for the top-level website map generation button is:
Protected void BtnGenerateSitemap_Click (object sender, EventArgs e)
{
CreateHtmlSitemap ();
CreateXMLSitemap ();
WebUtility. ResponseScript ("website map generated! ", 0 );
}
The code above is clear at a glance. createHtmlSitemap () is used to generate html website maps, while createXMLSitemap () is used to generate XML website maps.
Ii. Html website map generation
First, we will introduce the generation of Html website maps. The code for createHtmlSitemap () is as follows:
Public void createHtmlSitemap ()
{
FileInfo HtmlFile = null;
StreamWriter WriteHtmlFile = null;
String FilePath = Server. MapPath ("sitemap.html"); // obtain the path and name of the html file.
HtmlFile = new FileInfo (FilePath); // create an html file
WriteHtmlFile = HtmlFile. CreateText ();
WriteHtmlFile. WriteLine ("WriteHtmlFile. WriteLine ("WriteHtmlFile. WriteLine ("<meta http-equiv = Content-Type content = text/html; charset = UTF-8> ");
WriteHtmlFile. WriteLine ("<title> map of the English rabbit website </title> ");
WriteHtmlFile. WriteLine ("WriteHtmlFile. WriteLine ("<body> ");
GetHtmlSitemapData (WriteHtmlFile );
WriteHtmlFile. WriteLine ("</body> ");
WriteHtmlFile. WriteLine ("WriteHtmlFile. Close ();
}
The preceding Code creates the HTML file sitemap.html, and uses the object streamwriterbook to write sitemap.html. Gethtmlsitemapdata(writehtmlfile#complete the organization of the body section of sitemap.html.
Note that charset must be set to UTF-8 if the website map has Chinese characters. Otherwise, Chinese characters cannot be displayed normally.
Public void getHtmlSitemapData (StreamWriter writerFile)
{
String classTableName = "ArticleClass ";
String articleItemTableName = "ArticleItem ";
String connectionString = ConfigurationManager. etettings ["SQLConnString"]. ToString ();
SqlConnection conn = new SqlConnection (connectionString );
If (conn. State = ConnectionState. Closed)
{
Conn. Open ();
}
// Query the category Information
SqlDataAdapter classDataAdpt = new SqlDataAdapter ("SELECT Id, Classname FROM" + classTableName, connectionString );
DataSet classDataSet = new DataSet ();
ClassDataAdpt. Fill (classDataSet, classTableName );
WriterFile. WriteLine ("<table border = '1' width = '1000px 'cellspacing = '0' cellpadding = '0'> ");
// Query the document information of each category based on the category Information
For (int I = 0; I <classDataSet. Tables [0]. Rows. Count; I ++)
{
String tempsql = "SELECT Id, ArticleTitle, Parentid FROM" + articleItemTableName + "where Parentid =" + classDataSet. Tables [0]. Rows [I] [0]. ToString ();
SqlCommand cmd = new SqlCommand (tempsql, conn );
If (conn. State = ConnectionState. Closed)
{
Conn. Open ();
}
SqlDataReader rdr = cmd. ExecuteReader (CommandBehavior. CloseConnection );
If (rdr. HasRows)
{
Int classId = 0;
Int rowNum = 0; // records the information of an article.
Int memberInLine = 5; // The link information of the number of articles displayed on each line
While (rdr. Read ())
{
// Process large-class information. Large-class html files must also be written as hyperlinks.
If (classId! = Int. Parse (rdr [2]. ToString ()))
{
RowNum = 0;
ClassId = int. Parse (rdr [2]. ToString ());
WriterFile. WriteLine ("<tr> ");
WriterFile. WriteLine ("<td colspan = '5'> ");
WriterFile. WriteLine ("<a style = 'font-size: 15px; font-weight: bold; 'target = '_ blank' href = 'HTTP: // www.chituwang.com/Article/index.aspx? Parentid = "+ classDataSet. tables [0]. rows [I] [0]. toString () + "'>" + classDataSet. tables [0]. rows [I] [1]. toString () + "</a> ");
WriterFile. WriteLine ("</td> ");
WriterFile. WriteLine ("</tr> ");
}
// Process Document Information
RowNum ++;
If (1 = (rowNum % memberInLine) // a new line
{
WriterFile. WriteLine ("<tr> ");
}
WriterFile. WriteLine ("<td style = 'width: 200px '> ");
WriterFile. WriteLine ("<a target = '_ blank' href = 'HTTP: // www.chituwang.com/Article/index.aspx? Articleid = "+ rdr [0]. ToString () +" '> "+ rdr [1]. ToString () +" </a> ");
WriterFile. WriteLine ("</td> ");
If (0 = (rowNum % memberInLine) // a new line
{
WriterFile. WriteLine ("</tr> ");
}
}
}
Rdr. Close ();
}
WriterFile. WriteLine ("</table> ");
}
Iii. XML website map generation
The difference between XML website map generation and Html is that Google and other search engines have strict requirements on the format of this website map, which must be followed; otherwise, they will not recognize it. For specific xml file formats, see http://www.chituwang.com/Article/WZJS/detail.aspx? Articleid = 18092
The createXMLSitemap () function is implemented as follows:
Public void createXMLSitemap ()
{
FileInfo XMLFile = null;
StreamWriter WriteXMLFile = null;
String FilePath = Server. MapPath ("sitemap. xml ");
XMLFile = new FileInfo (FilePath );
WriteXMLFile = XMLFile. CreateText ();
// The following two sentences must be written and cannot be modified.
WriteXMLFile. WriteLine ("<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> ");
WriteXMLFile. WriteLine ("<urlset xmlns = \" http://www.google.com/schemas/sitemap/0.84\ "> ");
GetXMLSitemapData (WriteXMLFile );
WriteXMLFile. WriteLine ("</urlset>"); // do not forget this sentence.
WriteXMLFile. Close ();
}
CreateXMLSitemap () is implemented as follows:
Public void getXMLSitemapData (StreamWriter writerFile)
{
String classTableName = "ArticleClass ";
String articleItemTableName = "ArticleItem ";
String connectionString = ConfigurationManager. etettings ["SQLConnString"]. ToString ();
SqlConnection conn = new SqlConnection (connectionString );
If (conn. State = ConnectionState. Closed)
{
Conn. Open ();
}
// Query the category Information
SqlDataAdapter classDataAdpt = new SqlDataAdapter ("SELECT Id, Classname FROM" + classTableName, connectionString );
DataSet classDataSet = new DataSet ();
ClassDataAdpt. Fill (classDataSet, classTableName );
For (int I = 0; I <classDataSet. Tables [0]. Rows. Count; I ++)
{
String tempsql = "SELECT Id, ArticleTitle, Parentid FROM" + articleItemTableName + "where Parentid =" + classDataSet. Tables [0]. Rows [I] [0]. ToString ();
SqlCommand cmd = new SqlCommand (tempsql, conn );
If (conn. State = ConnectionState. Closed)
{
Conn. Open ();
}
SqlDataReader rdr = cmd. ExecuteReader (CommandBehavior. CloseConnection );
If (rdr. HasRows)
{
Int subClassId = 0;
While (rdr. Read ())
{
// Process information of a category,
If (subClassId! = Int. Parse (rdr [0]. ToString ()))
{
SubClassId = int. Parse (rdr [0]. ToString ());
WriterFile. WriteLine ("<url> ");
WriterFile. WriteLine ("<loc>" + "http://www.chituwang.com/Article/index.aspx? Parentid = "+ rdr [2]. ToString () +" </loc> ");
WriterFile. WriteLine ("<lastmod>" + DateTime. Now. tow.datestring () + "</lastmod> ");
WriterFile. WriteLine ("<changefreq> daily </changefreq> ");
WriterFile. WriteLine ("<priority> 0.5 </priority> ");
WriterFile. WriteLine ("</url> ");
}
// Process Document Information
WriterFile. WriteLine ("<url> ");
WriterFile. WriteLine ("<loc>" + "http://www.chituwang.com/Article/index.aspx? Articleid = "+ rdr [0]. ToString () +" </loc> ");
WriterFile. WriteLine ("<lastmod>" + DateTime. Now. tow.datestring () + "</lastmod> ");
WriterFile. WriteLine ("<changefreq> monthly </changefreq> ");
WriterFile. WriteLine ("<priority> 0.4 </priority> ");
WriterFile. WriteLine ("</url> ");
}
}
Rdr. Close ();
}
}
Through the introduction in the above two sections, website maps are generated in the form of web pages and XML.
4. Submit a website Map
Submit a website map to Google:
Https://www.google.com/webmasters/tools/login? Hl = zh_CN
1. log on to the website using your Google account and add your website;
2. Verify your website according to Google's requirements. You can select html file verification.
3. At this time, according to Google's requirements, create an html file with a file name required by Google
4. Use FTP to upload the html file to the root directory of your website together with the website map file in xml format
5. Confirm the verification and go back to the console to add your map file. Soon, Google will update your Website Based on the website Map File
Submit a website map to yahoo:
Submit the English yahoo website:
Http://search.yahoo.com/info/submit.html
First, use FTP to talk about the root directory that your website map uploads to the website. Open the website, select Submit Your Site for Free: log on to Your yahoo account, and enter the URL of Your website map.
Chinese Yahoo submission URL:
Http://sitemap.cn.yahoo.com/mysites
This operation is similar to Google's. You can log on to, verify the website, and submit the URL map.
Submit site map Sitemap to MSN: submit directly with URL: http://api.moreover.com/ping? U = http://your.domainname/sitemap.xml
This is the backdoor URL for submitting a website map directly to MSN.
Submit the website map Sitemap to ASK. Http://submissions.ask.com/ping? Sitemap = http://your.domainname/sitemap.xml
Submit address to online shop Alliance: http://www.dianmeng.com/
Submitting site map Sitemap to Baidu: No way, Baidu does not support Sitemap now. However, you can submit your website via http://www.baidu.com/search/url_submit.html.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.