.net 產生網站地圖 sitemap.xml

來源:互聯網
上載者:User

標籤:sitemap.xml   .net 產生網站地圖   

1.此處為Demo,借鑒別人的產生 xml
//建立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);         //建立三個子項目的子項目          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")); //儲存      }     //建立xml檔案方法二     protected void btn2_OnClick(object sender, EventArgs e)     {          XmlDocument xmldoc = new XmlDocument(); //建立空的XML文檔          xmldoc.LoadXml("<?xml version='1.0' encoding='gb2312'?>" +          "<bookstore>" +          "<book genre='fantasy' ISBN='2-3631-4'>" +          "<title>Oberon's Legacy</title>" +          "<author>Corets, Eva</author>" +          "<price>5.95</price>" +          "</book>" +          "</bookstore>");          xmldoc.Save(Server.MapPath("bookstore2.xml")); //儲存      } 

如果如下:

<?xml version="1.0" encoding="gb2312" ?>  <bookstore>   <book genre="fantasy" ISBN="2-3631-4">   <title>Oberon's Legacy</title>    <author>Corets, Eva</author>    <price>5.95</price>    </book> </bookstore>


不是我想要的網站地圖xml檔案。


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

using System;using System.Collections.Generic;using System.IO;using System.Threading;using System.Xml;namespace Helper{    public class SitemapXml    {        private const string Xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";        private const string XmlnsXsi = "http://www.w3.org/2001/XMLSchema-instance";        private const string XsiSchemaLocation = "http://www.sitemaps.org/schemas/sitemap/0.9  http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd";        /// <summary>        /// 產生SiteMap地圖        /// </summary>        /// <param name="siteMaps">需要產生的 對象列表</param>        /// <param name="saveFileName">設定檔案儲存名稱</param>        /// <param name="changefreq">更新周期</param>        /// <param name="savePath">xml檔案儲存路徑</param>        /// <returns></returns>        public static bool CreateSiteMapXml(List<SiteMap> siteMaps, string savePath = "/", string saveFileName = "sitemap", string changefreq = "weekly")        {            //儲存建立好的XML文檔            string filename = saveFileName + ".xml";            string path = System.Web.HttpContext.Current.Server.MapPath(savePath) + filename;            //先建立XML,返迴路徑            var xmldoc = new XmlDocument();            //加入XML的聲明段落,<?xml version="1.0" encoding="gb2312"?>            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");//尋找<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");//建立一個<url>節點                 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);//添加到<urlset>節點中             }            try            {                //然後在儲存到源位置                xmldoc.AppendChild(xmlelem);                xmldoc.Save(path);                return true;            }            catch (Exception)            {                return false;            }        }            }    /// <summary>    ///     /// <url>    /// <loc>http://news.chinahbnet.com/2014/8/19/15352.html</loc>    /// <priority>0.5</priority>    /// <lastmod>2014-08-19</lastmod>    /// <changefreq>weekly</changefreq>    /// </url>    ///     /// </summary>    public class SiteMap    {        /// <summary>        /// 連結地址        /// 如:http://news.chinahbnet.com        /// </summary>        public string Loc { get; set; }        /// <summary>        /// 網頁權重        /// 0.1 - 1        /// </summary>        public string Priority { get; set; }        /// <summary>        /// 產生日期        /// 2014-08-19        /// </summary>        public string Lastmod { get; set; }        /// <summary>        /// 更新周期        /// always  經常        /// hourly  每小時        /// daily   每天        /// weekly  每周        /// monthly 每月        /// yearly  每年        /// never   從不        /// </summary>        public string Changefreq { get; set; }           }}
 

產生的結果為:

<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9  http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">  <url>    <loc>http://www.baidu.com</loc>    <priority>1.00</priority>    <lastmod>2014-08-19</lastmod>    <changefreq>weekly</changefreq>  </url></urlset>



希望可以協助到你!




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.