首页 > 代码库 > .net 生成网站地图 sitemap.xml
.net 生成网站地图 sitemap.xml
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>
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 = http://www.mamicode.com/Xmlns;>生成的结果为:
<?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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。