三角阀厂家
免费服务热线

Free service

hotline

010-00000000
三角阀厂家
热门搜索:
行业资讯
当前位置:首页 > 行业资讯

net生成网站地图sitemapxml

发布时间:2020-03-09 14:26:39 阅读: 来源:三角阀厂家

1.此处为Demo,借鉴别人的生成 xml

//创建xml文件方法一

protected void btn1_OnClick(object sender, EventArgs e)

{

XmlText xmltext;

XmlDocument xmldoc = new XmlDocument();

//加入XML的声明段落

XmlNode xmlnode = eateXmlDeclaration("1.0", "gb2312", null);

pendChild(xmlnode);

//加入一个根元素

XmlElement xmlelem = eateElement("", "bookstore", "");

xmltext = eateTextNode("");

pendChild(xmltext);

pendChild(xmlelem);

//加入一个子元素

XmlElement xmlelem1 = eateElement("", "book", "");

xmltext = eateTextNode("");

pendChild(xmltext);

//为子元素"book"增加两个属性

tAttribute("genre", "", "fantasy");

tAttribute("ISBN", "2-3631-4");

em(1).AppendChild(xmlelem1);

//创建三个子元素的子元素

XmlElement xmlelem2 = eateElement("", "title", "");

xmltext = eateTextNode("Oberon's Legacy");

pendChild(xmltext);

em(1).AppendChild(xmlelem1).AppendChild(xmlelem2);

XmlElement xmlelem3 = eateElement("", "author", "");

xmltext = eateTextNode("Corets, Eva");

pendChild(xmltext);

em(1).AppendChild(xmlelem1).AppendChild(xmlelem3);

XmlElement xmlelem4 = eateElement("", "price", "");

xmltext = eateTextNode("5.95");

pendChild(xmltext);

em(1).AppendChild(xmlelem1).AppendChild(xmlelem4);

ve(pPath("l")); //保存

}

//创建xml文件方法二

protected void btn2_OnClick(object sender, EventArgs e)

{

XmlDocument xmldoc = new XmlDocument(); //创建空的XML文档

adXml("" +

"" +

"" +

"" +

"Corets, Eva" +

"5.95" +

"" +

"");

ve(pPath("l")); //保存

}

如果如下:

Corets, Eva

5.95

不是我想要的网站地图xml文件。

2.以下是我自己根据实际情况写的

using System;

using neric;

using ;

using reading;

using l;

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")

{

//保存创建好的XML文档

string filename = saveFileName + ".xml";

string path = pPath(savePath) + filename;

//先创建XML,返回路径

var xmldoc = new XmlDocument();

//加入XML的声明段落,

XmlDeclaration xmldecl = eateXmlDeclaration("1.0", "UTF-8", null);

pendChild(xmldecl);

//加入一个根元素

XmlNode xmlelem = eateElement("", "urlset", "");

//添加属性

XmlAttribute attr = eateAttribute("xmlns");

lue = Xmlns;

if (tributes != null) tNamedItem(attr);

attr = eateAttribute("xmlns:xsi");

lue = XmlnsXsi;

if (tributes != null) tNamedItem(attr);

attr = eateAttribute("xsi:schemaLocation");

lue = XsiSchemaLocation;

if (tributes != null) tNamedItem(attr);

pendChild(xmlelem);

string lastmod = String("yyyy-MM-dd");

for (int i = 0; i < unt; i++)

{

XmlNode root = lectSingleNode("urlset");//查找

if (root == null)

{

//加入一个根元素

xmlelem = eateElement("", "urlset", "");

//添加属性

attr = eateAttribute("xmlns");

lue = Xmlns;

if (tributes != null) tNamedItem(attr);

attr = eateAttribute("xmlns:xsi");

lue = XmlnsXsi;

if (tributes != null) tNamedItem(attr);

attr = eateAttribute("xsi:schemaLocation");

lue = XsiSchemaLocation;

if (tributes != null) tNamedItem(attr);

pendChild(xmlelem);

i = 0;

continue;

}

XmlElement xe1 = eateElement("url");//创建一个节点

XmlElement xmlelem1 = eateElement("", "loc", "");

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

pendChild(xmltext);

pendChild(xmlelem1);

xmlelem1 = eateElement("", "priority", "");

xmltext = eateTextNode(siteMaps[i].Priority);

pendChild(xmltext);

pendChild(xmlelem1);

xmlelem1 = eateElement("", "lastmod", "");

xmltext = eateTextNode(lastmod);

pendChild(xmltext);

pendChild(xmlelem1);

xmlelem1 = eateElement("", "changefreq", "");

xmltext = eateTextNode(changefreq);

pendChild(xmltext);

pendChild(xmlelem1);

pendChild(xe1);//添加到节点中

}

try

{

//然后在保存到源位置

pendChild(xmlelem);

ve(path);

return true;

}

catch (Exception)

{

return false;

}

}

}

///

///

///

///

///0.5

///2014-08-19

///weekly

///

///

///

public class SiteMap

{

///

/// 链接地址

/// 如:

///

public string Loc { get; set; }

///

/// 网页权重

/// 0.1 - 1

///

public string Priority { get; set; }

///

/// 生成日期

/// 2014-08-19

///

public string Lastmod { get; set; }

///

/// 更新周期

/// always 经常

/// hourly 每小时

/// daily 每天

/// weekly 每周

/// monthly 每月

/// yearly 每年

/// never 从不

///

public string Changefreq { get; set; }

}

}

生成的结果为:

1.00

2014-08-19

weekly

希望可以帮助到你!

SEO专题推荐:

关键词优化专题:网站关键词优化没效果?来这里学习最实用的关键词优化技巧!

内链优化专题:最能提升网站权重的内链部署优化技巧与方法

外链建设专题:高质量自然外链怎么做?读完这些你将质的飞跃

网站降权专题:2015年最有用的网站降权、被K、被黑、被攻击的解决方法

用户体验专题:学习完这些,作为站长的你可以秒懂如何做网站用户体验

行业网站专题:优化行业网站的“葵花宝典”看完后无优化压力

整容整形咨询专家

隆鼻整形报价

专业隆鼻整容价格

专业鼻子整形优惠活动