Asp. NET to generate Google Sitemap Code _ Practical Skills

Source: Internet
Author: User
Copy Code code as follows:

<summary>
Create a Google site 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");
Traverse all files on the scan site
Showfiles (RootDirectory, Writer);
Writer.writeendelement ();
Writer.writeenddocument ();
Writer.close ();
return true;
}
catch (Exception err)
{
return false;
}
}
Traverse all files on the scan site
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.appsettings["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: Often, hourly: hours, daily: Days, Weekly: Weeks, Monthly: months, yearly: Years
Writer.writeendelement ();
Writer.writestartelement ("priority");
Writer.writestring ("0.8");//precedence over other pages, this value is set between 0.0-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.