国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

18143453325 在線咨詢 在線咨詢
18143453325 在線咨詢
所在位置: 首頁 > 營銷資訊 > 建站知識 > .net 生成網(wǎng)站地圖 sitemap.xml

.net 生成網(wǎng)站地圖 sitemap.xml

時間:2022-07-18 14:21:01 | 來源:建站知識

時間:2022-07-18 14:21:01 來源:建站知識

1.此處為Demo,借鑒別人的生成 xml

//創(chuàng)建xml文件方法一

protected void btn1_OnClick(object sender, EventArgs e)

{

XmlText xmltext;

XmlDocument xmldoc = new XmlDocument();

//加入XML的聲明段落

XmlNode xmlnode = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);

xmldoc.AppendChild(xmlnode);

//加入一個根元素

XmlElement xmlelem = xmldoc.CreateElement("", "bookstore", "");

xmltext = xmldoc.CreateTextNode("");

xmlelem.AppendChild(xmltext);

xmldoc.AppendChild(xmlelem);

//加入一個子元素

XmlElement xmlelem1 = xmldoc.CreateElement("", "book", "");

xmltext = xmldoc.CreateTextNode("");

xmlelem1.AppendChild(xmltext);

//為子元素"book"增加兩個屬性

xmlelem1.SetAttribute("genre", "", "fantasy");

xmlelem1.SetAttribute("ISBN", "2-3631-4");

xmldoc.ChildNodes.Item(1).AppendChild(xmlelem1);

//創(chuàng)建三個子元素的子元素

XmlElement xmlelem2 = xmldoc.CreateElement("", "title", "");

xmltext = xmldoc.CreateTextNode("Oberon's Legacy");

xmlelem2.AppendChild(xmltext);

xmldoc.ChildNodes.Item(1).AppendChild(xmlelem1).AppendChild(xmlelem2);

XmlElement xmlelem3 = xmldoc.CreateElement("", "author", "");

xmltext = xmldoc.CreateTextNode("Corets, Eva");

xmlelem3.AppendChild(xmltext);

xmldoc.ChildNodes.Item(1).AppendChild(xmlelem1).AppendChild(xmlelem3);

XmlElement xmlelem4 = xmldoc.CreateElement("", "price", "");

xmltext = xmldoc.CreateTextNode("5.95");

xmlelem4.AppendChild(xmltext);

xmldoc.ChildNodes.Item(1).AppendChild(xmlelem1).AppendChild(xmlelem4);

xmldoc.Save(Server.MapPath("bookstore.xml")); //保存

}

//創(chuàng)建xml文件方法二

protected void btn2_OnClick(object sender, EventArgs e)

{

XmlDocument xmldoc = new XmlDocument(); //創(chuàng)建空的XML文檔

xmldoc.LoadXml("" +

"" +

"" +

"" +

"Corets, Eva" +

"5.95" +

"" +

"");

xmldoc.Save(Server.MapPath("bookstore2.xml")); //保存

}

如果如下:

Corets, Eva

5.95

不是我想要的網(wǎng)站地圖xml文件。

2.以下是我自己根據(jù)實際情況寫的

using System;

using System.Collections.Generic;

using System.IO;

using System.Threading;

using System.Xml;

namespace Helper

{

public class SitemapXml

{

private const string Xmlns = "";

private const string XmlnsXsi = "";

private const string XsiSchemaLocation = " ";

///

/// 生成SiteMap地圖

///

///需要生成的 對象列表

///設置文件保存名稱

///更新周期

///xml文件保存路徑

///

public static bool CreateSiteMapXml(ListsiteMaps, string savePath = "/", string saveFileName = "sitemap", string changefreq = "weekly")

{

//保存創(chuàng)建好的XML文檔

string filename = saveFileName + ".xml";

string path = System.Web.HttpContext.Current.Server.MapPath(savePath) + filename;

//先創(chuàng)建XML,返回路徑

var xmldoc = new XmlDocument();

//加入XML的聲明段落,

XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);

xmldoc.AppendChild(xmldecl);

//加入一個根元素

XmlNode xmlelem = xmldoc.CreateElement("", "urlset", "");

//添加屬性

XmlAttribute attr = xmldoc.CreateAttribute("xmlns");

attr.Value = Xmlns;

if (xmlelem.Attributes != null) xmlelem.Attributes.SetNamedItem(attr);

attr = xmldoc.CreateAttribute("xmlns:xsi");

attr.Value = XmlnsXsi;

if (xmlelem.Attributes != null) xmlelem.Attributes.SetNamedItem(attr);

attr = xmldoc.CreateAttribute("xsi:schemaLocation");

attr.Value = XsiSchemaLocation;

if (xmlelem.Attributes != null) xmlelem.Attributes.SetNamedItem(attr);

xmldoc.AppendChild(xmlelem);

string lastmod = DateTime.Now.ToString("yyyy-MM-dd");

for (int i = 0; i < siteMaps.Count; i++)

{

XmlNode root = xmldoc.SelectSingleNode("urlset");//查找

if (root == null)

{

//加入一個根元素

xmlelem = xmldoc.CreateElement("", "urlset", "");

//添加屬性

attr = xmldoc.CreateAttribute("xmlns");

attr.Value = Xmlns;

if (xmlelem.Attributes != null) xmlelem.Attributes.SetNamedItem(attr);

attr = xmldoc.CreateAttribute("xmlns:xsi");

attr.Value = XmlnsXsi;

if (xmlelem.Attributes != null) xmlelem.Attributes.SetNamedItem(attr);

attr = xmldoc.CreateAttribute("xsi:schemaLocation");

attr.Value = XsiSchemaLocation;

if (xmlelem.Attributes != null) xmlelem.Attributes.SetNamedItem(attr);

xmldoc.AppendChild(xmlelem);

i = 0;

continue;

}

XmlElement xe1 = xmldoc.CreateElement("url");//創(chuàng)建一個節(jié)點

XmlElement xmlelem1 = xmldoc.CreateElement("", "loc", "");

XmlText xmltext = xmldoc.CreateTextNode(siteMaps[i].Loc);

xmlelem1.AppendChild(xmltext);

xe1.AppendChild(xmlelem1);

xmlelem1 = xmldoc.CreateElement("", "priority", "");

xmltext = xmldoc.CreateTextNode(siteMaps[i].Priority);

xmlelem1.AppendChild(xmltext);

xe1.AppendChild(xmlelem1);

xmlelem1 = xmldoc.CreateElement("", "lastmod", "");

xmltext = xmldoc.CreateTextNode(lastmod);

xmlelem1.AppendChild(xmltext);

xe1.AppendChild(xmlelem1);

xmlelem1 = xmldoc.CreateElement("", "changefreq", "");

xmltext = xmldoc.CreateTextNode(changefreq);

xmlelem1.AppendChild(xmltext);

xe1.AppendChild(xmlelem1);

root.AppendChild(xe1);//添加到節(jié)點中

}

try

{

//然后在保存到源位置

xmldoc.AppendChild(xmlelem);

xmldoc.Save(path);

return true;

}

catch (Exception)

{

return false;

}

}

}

///

///

///

///

///0.5

///2014-08-19

///weekly

///

///

///

public class SiteMap

{

///

/// 鏈接地址

/// 如:

///

public string Loc { get; set; }

///

/// 網(wǎng)頁權重

/// 0.1 - 1

///

public string Priority { get; set; }

///

/// 生成日期

/// 2014-08-19

///

public string Lastmod { get; set; }

///

/// 更新周期

/// always 經(jīng)常

/// hourly 每小時

/// daily 每天

/// weekly 每周

/// monthly 每月

/// yearly 每年

/// never 從不

///

public string Changefreq { get; set; }

}

}

生成的結果為:

1.00

2014-08-19

weekly

希望可以幫助到你!

SEO專題推薦:

關鍵詞優(yōu)化專題:網(wǎng)站關鍵詞優(yōu)化沒效果?來這里學習最實用的關鍵詞優(yōu)化技巧!

內(nèi)鏈優(yōu)化專題:最能提升網(wǎng)站權重的內(nèi)鏈部署優(yōu)化技巧與方法

外鏈建設專題:高質量自然外鏈怎么做?讀完這些你將質的飛躍

網(wǎng)站降權專題:2015年最有用的網(wǎng)站降權、被K、被黑、被攻擊的解決方法

用戶體驗專題:學習完這些,作為站長的你可以秒懂如何做網(wǎng)站用戶體驗

行業(yè)網(wǎng)站專題:優(yōu)化行業(yè)網(wǎng)站的“葵花寶典”看完后無優(yōu)化壓力

關鍵詞:地圖

74
73
25
news

版權所有? 億企邦 1997-2022 保留一切法律許可權利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關閉