/// <summary>
/// 描述:自定义站点地图类,通用站点地图
/// 日期:2013.9.30
/// </summary>
public class SiteMap
{
#region 私有变量
private XmlDocument m_Doc = new XmlDocument();
private string m_XmlPath = HttpContext.Current.Server.MapPath("/config/SiteMap.xml");
private int m_xPathIndex = 0;
private string[] m_xPath = new string[0];
private string m_currxPath = "";
private string m_MapIcon = "》";
#endregion
#region 普通常量
private const char m_Net = '/';
private const string m_Name = "name";
private const string m_Title = "title";
private const string m_Url = "url";
#endregion
#region 构造函数
public SiteMap()
{
LoadXML();
}
public SiteMap(string path)
{
this.m_XmlPath = path;
LoadXML();
}
public SiteMap(string path, string mapicon)
{
this.m_XmlPath = path;
this.m_MapIcon = mapicon;
LoadXML();
}
#endregion
#region 加载站点地图
private void LoadXML()
{
m_Doc.Load(m_XmlPath);
}
#endregion
#region 模板 html代码
private string Templete(string url, string name, string title, bool isIcon = true)
{
string tempStr;
if (isIcon)
tempStr = string.Format("<a class=\"mapname\" href=\"{0}\" title=\"{1}\">{2}</a><span class=\"mapicon\">{3}</span>", url, title, name, m_MapIcon);
else
tempStr = string.Format("<a class=\"mapname\" href=\"{0}\" title=\"{1}\">{2}</a>", url, title, name);
return tempStr;
}
#endregion
#region 递归获取站点地图
private string MakeSiteMap(XmlNode node)
{
StringBuilder tempStr = new StringBuilder();
bool isLast = false;
isLast = m_xPathIndex < m_xPath.Length;
XmlElement xmlEle = node as XmlElement;
string Name = xmlEle.GetAttribute(m_Name);
if (!string.IsNullOrEmpty(Name))
if (isLast)
tempStr.Append(Templete(xmlEle.GetAttribute(m_Url), Name, xmlEle.GetAttribute(m_Title)));
else
tempStr.Append(Templete(xmlEle.GetAttribute(m_Url), Name, xmlEle.GetAttribute(m_Title), false));
if (isLast)
{
m_currxPath += m_Net + m_xPath[m_xPathIndex++];
XmlNode parentNode = m_Doc.SelectSingleNode(m_currxPath);
tempStr.Append(MakeSiteMap(parentNode));
}
return tempStr.ToString();
}
#endregion
#region 入口处调用唯一地址,传入XPath路径 /sitemap/admin/或者/sitemap/web/
public string MakeSiteMap(string _xmlpath)
{
m_xPathIndex = 0;
if (_xmlpath[0] == m_Net)
_xmlpath = _xmlpath.Substring(1, _xmlpath.Length - 1);
m_xPath = _xmlpath.Split(m_Net);
m_currxPath = m_xPath[m_xPathIndex++] + m_Net + m_xPath[m_xPathIndex++];
XmlNode parentNode = m_Doc.SelectSingleNode(m_currxPath);//最父级
return MakeSiteMap(parentNode);
}
#endregion
#region 调用方法
/* SiteMap siteMap = new SiteMap()
* /SiteMap siteMap = new SiteMap(xml文件配置路径)
* /SiteMap siteMap = new SiteMap(xml文件配置路径,站点地图名字之间的分割符号);
* siteMap.MakeSiteMap("sitemap/admin/home/customer");
* siteMap.MakeSiteMap("sitemap/admin/home/customer/add");
* siteMap.MakeSiteMap("sitemap/admin/home/customer/update");
*/
#endregion
}
/****************xml内容***********************/
<?xml version="1.0" encoding="utf-8" ?>
<sitemap>
<admin>
<home url="/home" name="奇乐TV" title="">
<customer url="/user/index" name="用户管理" title="">
<add url="/user/add" name="添加用户" title=""></add>
<update url="/user/update" name="修改用户" title=""></update>
</customer>
</home>
</admin>
<web></web>
</sitemap>