asp.net C# 生成sitemap.xml 问题?

[复制链接]
查看11 | 回复0 | 2009-12-3 13:13:29 | 显示全部楼层 |阅读模式
给你一点参考吧~~这是我以前写的一个动态生成站点地图的~~一个简单实例方法~~~~~~你可以看一下~~~注释很清楚首先前台将Web.SitMap的物理路径传入CreatSiteMap(string path)方法就可以了~[string path=Server.MapPath("~/Web.sitemap");]其次关于数据库这边我就是通过GetList(string strWhere)提供的DataTable方法来实现的当然这里根据自己需要来设计数据库不过至少要提供Web.SitMap的三个基本属性~~~当然需要更多的自己可以在ChildNodesCreate(ref XmlDocument doc,ref XmlElement t,DataRow row)方法内添加就行了注释的很清楚~~
///
/// 查询多条数据
///
/// 要查询的条件
/// 返回一个DataTbale查询集合
public DataTable GetList(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * from company_title ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
returnDBLink.ToDataTable(DBLink.ExecuteReader(strSql.ToString()));
}
///
/// 生成Web.Sitemap站点地图的的方法
///
/// 站点地图所在物理路径
/// 生成是否成功
public bool CreatSiteMap(string path)
{
AddWebSiteMap(path);//创建XML
XmlDocument doc = new XmlDocument();
doc.Load(path);//获取XML对象
XmlNode node = doc.DocumentElement;//创建根节点
//创建根节点的一个属性
XmlAttribute mains = doc.CreateAttribute("xmlns");
mains.Value = "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0";
node.Attributes.Append(mains);//将属性添加到根节点
//查询根节点(首页)
string strWhere = " titleid=0 and titlevisible=0 and titletype=0 ";
DataTable table=GetList(strWhere);
/////////////////
for (int i = 0; i
/// 递归创建节点及其子节点
///
/// XML对象
/// 子节点
/// 数据库中取出的要添加的节点对象
/// 是否添加成功
public bool ChildNodesCreate(ref XmlDocument doc,ref XmlElement t,DataRow row)
{
//添加Web.SiteMap的三个基本属性
XmlAttribute nodvalue = doc.CreateAttribute("url");
nodvalue.Value = row["TitleUrl"].ToString();
t.Attributes.Append(nodvalue);
nodvalue = doc.CreateAttribute("title");
nodvalue.Value = row["TitleName"].ToString();
t.Attributes.Append(nodvalue);
nodvalue = doc.CreateAttribute("description");
nodvalue.Value = row["TitleName"].ToString();
t.Attributes.Append(nodvalue);
//取出该节点的所有子节点
string strWhere = " TitleFID=" + row["TitleID"].ToString() + " and titlevisible=0 and TitleID0";
DataTable table = GetList(strWhere);
//进行判断是否存在子节点并且进行循环递归调用
for (int i = 0;i[table]
/// 创建一个WEB.SiteMap
///
public void AddWebSiteMap(string path)
{
XmlWriter xw = XmlWriter.Create(path);
xw.WriteComment("动态生成的站点地图");
xw.WriteStartElement("siteMap");
xw.WriteEndElement();
xw.Close();
}上变已经有了给以个节点附加属性~~以及添加子节点问题~~~~呵呵~~其实就是简单的DOM解析了~~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行