个人站点地图类

     /// <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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值