Wriju's BLOG

.NET, Cloud and everything

Changing the SiteMapDataSource’s XML filename at runtime

 


Applies to: ASP.NET 2.0


 


ASP.NET 2.0 comes with set of very rich navigation controls like TreeView, Menu, SiteMapPath. Ideally these controls should load the values at runtime from any structured data sources, be it SQL or XML or TXT. The default and most commonly used provider is XmlSiteMapProvider which accepts the data from XML file. But if you have to load different XML files at runtime, then … Here we go


 


Create two web.sitemap files


 


File 1 [web.sitemap]


=============


<?xml version=1.0 encoding=utf-8 ?>


<siteMap>


      <siteMapNode title=MyMenu 1 url=Default.aspx>          


      </siteMapNode>


</siteMap>


 


File 1 [web2.sitemap]


==============


<?xml version=1.0 encoding=utf-8 ?>


<siteMap>


  <siteMapNode title=MyMenu 2 url=Default.aspx>


  </siteMapNode>


</siteMap>


 


 


Then put any navigation control in your aspx page with the SiteMapDataSource as data source.


 


The main trick is with the web.config settings. There you have to mention all the files as the provider collection like


<?xml version=1.0?>


<configuration>  


      <system.web>


            ………


            <siteMap defaultProvider=1SiteMap enabled=true>


                  <providers>


                        <add name=1SiteMap type=System.Web.XmlSiteMapProvider siteMapFile=Web.sitemap/>


                        <add name=2SiteMap type=System.Web.XmlSiteMapProvider siteMapFile=Web2.sitemap/>


                  </providers>


            </siteMap>


      </system.web>


 


Then in the page load event of your page (where the SiteMapDataSource and navigation controls are) change the property SiteMapProvider like


     


protected void Page_Load(object sender, EventArgs e)


    {


        //The provider name mentioned in the web.config


        SiteMapDataSource1.SiteMapProvider = “2SiteMap”;


    }


 


Hope this will help in simpler way, but it is recommended that you should use SqlSiteMapProvider for all dynamic scenarios.


 


Ref: http://weblogs.asp.net/scottgu/archive/2005/11/20/431019.aspx


 


Namoskar