Code for generating a Google website map using ASP. NET

Source: Internet
Author: User

CopyCode The Code is as follows: // <summary>
/// Generate a Google website Map
/// </Summary>
/// <Returns> </returns>
Public static boolbuildgooglesitemap ()
{
Try
{
String rootdirectory = appdomain. currentdomain. basedirectory;
Xmltextwriter writer = new xmltextwriter (httpcontext. Current. server. mappath ("~ /Googlesitemaps. xml "), encoding. getencoding (" UTF-8 "));
Writer. Formatting = formatting. indented;
Writer. writestartdocument ();
Writer. writestartelement ("urlset", "http://www.google.com/schemas/sitemap/0.84 ");
// Scan all files on the website
Showfiles (rootdirectory, writer );
Writer. writeendelement ();
Writer. writeenddocument ();
Writer. Close ();
Return true;
}
Catch (exception ERR)
{
Return false;
}
}
// Scan all files on the website
Static void showfiles (string dirpath, xmltextwriter writer)
{
Bool isread = true;
String [] notread = {"app_data", "bin", "FCKeditor", "JS", "MyAdmin", "powerchatroom"}; // exclude these folders
Foreach (string s in notread)
{
String dirname = dirpath. substring (dirpath. lastindexof (@ "\") + 1 );
If (dirname = s)
{
Isread = false;
Break;
}
}
If (! Isread)
Return;
Try
{
Directoryinfo dir = new directoryinfo (dirpath );
Foreach (fileinfo F in Dir. getfiles ())
{
String Path = dir. fullname. Replace (appdomain. currentdomain. basedirectory, ""); // file relative directory
// Httpcontext. Current. response. Write (appdomain. currentdomain. basedirectory + "**********" + dir. fullname + "<br> ");
Writer. writestartelement ("url ");
Writer. writestartelement ("Loc ");
Stringbuilder sb = new stringbuilder ("/" + path + "/" + F. Name );
SB. Replace ("//", "/"). Replace (@"\","/");
Writer. writestring (configurationmanager. receivettings ["websiteurl"]. tostring () + sb. tostring ());
Writer. writeendelement ();
Writer. writestartelement ("lastmod ");
Writer. writestring (string. Format ("{0: yyyy-mm-dd}", F. lastwritetime ));
Writer. writeendelement ();
Writer. writestartelement ("changefreq ");
Writer. writestring ("always"); // update frequency: Always: regular, hourly: hour, daily: Day, weekly: Week, monthly: Month, yearly: Year
Writer. writeendelement ();
Writer. writestartelement ("Priority ");
Writer. writestring ("0.8"); // priority relative to other pages. The value is between 0.0 and 1.0.
Writer. writeendelement ();
Writer. writeendelement ();
}
Foreach (directoryinfo D in Dir. getdirectories ())
{
Showfiles (D. fullname, writer );
}
}
Catch (exception ){}
}

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.