C # how to generate a sitemap

Source: Internet
Author: User

Sitemaps is a Google tool related to website administrators. It is similar to the RSS function of BLOG and is a convenient service. If you submit your own updates in this way, google no longer needs to send so many crawlers to go around. Any website will automatically "notify" Google as long as there is an update to facilitate Google indexing.

It seems that BAIDU has recently started to support sitemap sites in XML format.

Currently, there are many free tools on the network to generate sitemap site maps, which are also convenient to use. The principle is to capture the page you specified, get all the links on the page, and generate a sitemap site map file in xml format based on these links.

However, the disadvantage is that only sitemap with links on the page can be generated. At the same time, the site map must be manually uploaded to the server for use.

The following provides a method to automatically generate a sitemap site map through C #, which can be directly generated in the Server Directory through the website system, in addition, you can set any link that needs to be displayed on the sitemap site map as needed.

Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace Voodoo. other. SEO
{
/// <Summary>
/// Generate site map sitemap
// (C) http://aizr.net
/// </Summary>
Public class SiteMap
{
Public List <PageInfo> url
{
Get;
Set;
}

/// <Summary>
/// Generate the SiteMap string
/// </Summary>
/// <Returns> </returns>
Public string GenerateSiteMapString ()
{
StringBuilder sb = new StringBuilder ();
Sb. AppendLine ("<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> ");
Sb. AppendLine ("<urlset xmlns = \" http://www.sitemaps.org/schemas/sitemap/0.9.pdf ">");

Foreach (PageInfo pi in url)
{
Sb. AppendLine ("<url> ");
Sb. AppendLine (string. Format ("<loc >{0} </loc>", pi. loc ));
Sb. AppendLine (string. Format ("<lastmod> {0} </lastmod>", pi. lastmod. ToString ("yyyy-MM-dd ")));
Sb. AppendLine (string. Format ("<changefreq >{0} </changefreq>", pi. changefreq ));
Sb. AppendLine (string. Format ("<priority> {0} </priority>", pi. priority ));
Sb. AppendLine ("</url> ");
}

Sb. AppendLine ("</urlset> ");
Return sb. ToString ();
}

/// <Summary>
/// Save the Site file
/// </Summary>
/// <Param name = "FilePath"> path </param>
Public void SaveSiteMap (string FilePath)
{
Voodoo. IO. File. Write (FilePath, GenerateSiteMapString (); // save it in the specified directory
}
}

Public class PageInfo
{
/// <Summary>
/// URL
/// </Summary>
Public string loc {get; set ;}

/// <Summary>
/// Last Update Time
/// </Summary>
Public DateTime lastmod {get; set ;}

/// <Summary>
/// Update frequency
/// </Summary>
Public string changefreq {get; set ;}

/// <Summary>
/// Priority, 0-1
/// </Summary>
Public string priority {get; set ;}
}
}

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.