欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#生成站点地图(SiteMapPath)文件示例程序

程序员文章站 2024-02-16 23:01:01
复制代码 代码如下://创建站点地图        private void createsitema...

复制代码 代码如下:

//创建站点地图
        private void createsitemap(dataset ds)
        {

            xmldeclaration declareation;
            declareation = xmldoc.createxmldeclaration("1.0", "utf-8", null);
            xmldoc.appendchild(declareation);

            xmlelement xeroot = xmldoc.createelement("sitemap");
            xmldoc.appendchild(xeroot);

            xmlelement xroot = xmldoc.createelement("sitemapnode");
            xroot.setattribute("title", "");
            xroot.setattribute("url", "#");
            xeroot.appendchild(xroot);

            for (int i = 0; i <= ds.tables[0].rows.count - 1; i++)
            {
                datarowview row = ds.tables[0].defaultview[i];

                string mainmenu = row["mainmenu"].tostring();
                string navigateurl = row["navigateurl"].tostring();
                if (mainmenu != str)
                {
                    xmlelement sitemapnode = xmldoc.createelement("sitemapnode");
                    sitemapnode.setattribute("title", mainmenu);
                    sitemapnode.setattribute("description", "");
                    sitemapnode.setattribute("url", navigateurl);
                    xroot.appendchild(sitemapnode);
                    str = addchildnode(mainmenu);
                }
            }
            xmldoc.save(server.mappath("\\web.sitemap"));
        }

        //添加子节点
        private string addchildnode(string text)
        {
            string sql = "select * from menu where mainmenu ='" + text + "'";
            datasql data = new datasql();
            data.datacon();
            dataset ds = data.getdataset(sql);
            xmlnode root = xmldoc.selectsinglenode("/sitemap/sitemapnode/sitemapnode[@title='" + text + "']");
            for (int i = 0; i <= ds.tables[0].rows.count - 1; i++)
            {
                datarowview row = ds.tables[0].defaultview[i];

                string childmenu = row["childmenu"].tostring();
                if (childmenu != "")
                {
                    string navigateurl = row["navigateurl"].tostring();

                    xmlelement sitemapnode = xmldoc.createelement("sitemapnode");
                    sitemapnode.setattribute("title", childmenu);
                    sitemapnode.setattribute("description", "");
                    sitemapnode.setattribute("url", navigateurl);
                    root.appendchild(sitemapnode);
                }
            }
            return text;
        }