Based on C # Site Map Production _ Practical skills

Source: Internet
Author: User
Tags datetime
1, our website is developed with C #, we use the database to store all article information. So our articles are dynamically extracted from the database. This is not conducive to the spider's Clues.
2, this point, hehe, a little reluctant. Make a website map, can let the user to the content of our website at a glance, can play a very good navigation function.
In view of the above reasons, I decided to make a website map for my "Red rabbit English Net". As Google and other search engines need the site map must be an XML file, so I decided to do Web forms and XML forms of two Web site map. Our website is developed in C #, of course, the use of C # language to write my site map, illustrious.
First, let's take a look at how our site map should be generated and how it will be presented. After a simple thought, I decided to do so. Homepage of the menu to add the "sitemap" option, its link to the form of the Web site map, the page name for sitemap.html; the bottom of the homepage also add links to the site map, its chain followed by the XML format of the Sitemap, the page name is Sitemap.xml. The two page generators are added in the background. Set a button, the administrator in a period of time to click the button, directly generate these two files, placed in the root directory of the site, for the client to call at any time.
Next, let's take a look at the concrete implementation of the code that generates the site map. For the readability of the article, we introduce the code implementation process from top to bottom.
first, the site map of the top-level design
The code for the top-level site Map Generation button is:
protected void Btngeneratesitemap_click (object sender, EventArgs e)
{
Createhtmlsitemap ();
Createxmlsitemap ();
Webutility.responsescript ("Site map generation completed!") ", 0);
}
The code at a Glance, Createhtmlsitemap () is used to generate HTML sitemap, Createxmlsitemap () is used to generate XML site maps.
second, the generation of HTML site map
Let's first introduce the generation of HTML site maps. The code for CREATEHTMLSITEMAP () is as follows:
public void Createhtmlsitemap ()
{
FileInfo htmlfile = null;
StreamWriter writehtmlfile = null;
String FilePath = Server.MapPath ("sitemap.html")//Get HTML file path and name
Htmlfile = new FileInfo (FilePath);//Create 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> Red Rabbit English website map </title>");
Writehtmlfile.writeline ("Writehtmlfile.writeline ("<body>");
Gethtmlsitemapdata (Writehtmlfile);
Writehtmlfile.writeline ("</body>");
Writehtmlfile.writeline ("Writehtmlfile.close ();
}
The code above creates an HTML file sitemap.html and uses the object StreamWriter to write sitemap.html. function Gethtmlsitemapdata (Writehtmlfile) completes the organization of the sitemap.html body part.
It is important to note that if you have Chinese in the site map, you must set the CharSet to UTF-8. Otherwise the Chinese can not be displayed normally.
public void Gethtmlsitemapdata (StreamWriter writerfile)
{
String classtablename = "Articleclass";
String articleitemtablename = "Articleitem";
String connectionString = configurationmanager.appsettings["sqlconnstring"]. ToString ();
SqlConnection conn = new SqlConnection (connectionString);
IF (Conn. state = = connectionstate.closed)
{
Conn. Open ();
}
Query large categories of 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 ' >");
According to the large categories of information to check the various categories under the article Information
for (int i = 0; i < classdataset.tables[0]. Rows.Count; i++)
{
String tempsql = "Select Id, ArticleTitle, parentid from" + Articleitemtablename + "where ParentID =" + Classdataset.ta Bles[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 processed article information
int memberinline = 5;//How many posts are linked per line
while (RDR. Read ())
{
To handle large classes of information, large classes also need to write HTML files 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>");
}
Working with article 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>");
}
third, the generation of XML Web site map
The generation of XML Sitemap differs from HTML in that the search engines such as Google have strict requirements for the format of such site maps and must comply with them, otherwise they will not be recognized. The specific XML file format can refer to 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 words must be written, and can not make any changes
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>");/Don't forget this sentence.
Writexmlfile.close ();
}
The implementation of Createxmlsitemap () is as follows:
public void Getxmlsitemapdata (StreamWriter writerfile)
{
String classtablename = "Articleclass";
String articleitemtablename = "Articleitem";
String connectionString = configurationmanager.appsettings["sqlconnstring"]. ToString ();
SqlConnection conn = new SqlConnection (connectionString);
IF (Conn. state = = connectionstate.closed)
{
Conn. Open ();
}
Query large categories of 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.ta Bles[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 ())
{
Dealing with large categories of information,
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.ToShortDateString () + "</lastmod>");
Writerfile.writeline ("<changefreq>daily</changefreq>");
Writerfile.writeline ("<priority>0.5</priority>");
Writerfile.writeline ("</url>");
}
Working with article Information
Writerfile.writeline ("<url>");
Writerfile.writeline ("<loc>" + "http://www.chituwang.com/Article/index.aspx?articleid=" + rdr[0]. ToString () + "</loc>");
Writerfile.writeline ("<lastmod>" + DateTime.Now.ToShortDateString () + "</lastmod>");
Writerfile.writeline ("<changefreq>monthly</changefreq>");
Writerfile.writeline ("<priority>0.4</priority>");
Writerfile.writeline ("</url>");
}
}
Rdr. Close ();
}
}
With the introduction of the two sections above, Web pages and two forms of XML Web site maps are generated.
iv. submission of site map
Submit site map to Google:
Https://www.google.com/webmasters/tools/login?hl=zh_CN
1, enter this site, with your Google account login, and then add your URL;
2, next in accordance with Google's requirements to verify your site, you can select the HTML file validation.
3, at this time, according to Google's request, the production of a Google requested file name HTML file
4, using FTP to speak this HTML file and your XML format site map file to upload to your site's root directory
5, confirm the verification, return to the console, add your map file. Soon, Google will update your site based on site map files.
Submit site map to Yahoo:
English Yahoo submit Web site:
Http://search.yahoo.com/info/submit.html
First use FTP to tell your site map to the site's root directory. Open the URL, select Submit Your site for free: Login with Yahoo account, then enter your site map URL on it.
Chinese Yahoo's submission URL:
Http://sitemap.cn.yahoo.com/mysites
This operation and Google's similar, login, verify the site, submit URL map URLs.
Submit site Map to MSN Sitemap: Submit directly with URL: http://api.moreover.com/ping?u=http://your.domainname/sitemap.xml
This is the backdoor URL that submits the sitemap directly to MSN
Submit Site Map Sitemap: Submit directly. Http://submissions.ask.com/ping?sitemap=http://your.domainname/sitemap.xml
Submit address to online affiliate: http://www.dianmeng.com/
Baidu to submit site Map Sitemap: No way, now Baidu does not support Sitemap. However, you can submit your URL 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.