Accessing ChildNodes in BuildSiteMap of the XmlSiteMapProvider class
posted on Thursday, December 01, 2005 by bobby @ 1:47 am
0 comments
In a previous post, I was attempting to create my own SiteMapProvider. Derriving from XmlSiteMapProvider, I wanted to alter the nodes after the site map was built. New to ASP.NET 2.0, the most logical place to alter this was in the BuildSiteMap method.

After calling base.BuildSiteMap(), I attempted to access the RootNode.ChildNodes collection. To my demise, I was served a fatty StackOverflowException. Danny Chen from Microsoft was nice enough to explain why this happens. The following is an excerpt from our conversation:

The base design of the sitemap provider is supposed to allow for optimal retrieval of nodes. This means that if you request a particular node, the provider should retreive that node and only that node. But this can get tricky since the node has references to it's parent and child nodes The solution is to lazily retreive the parent/child nodes "on demand". This means that until you actually request those properties, they aren't populated. Now, there are ways to prefetch these if you know ahead of time but that's a different track.
One of the ways we've dealt with potential issues due to the tight coupling of these two objects (SiteMapProvider and SiteMapNode) is to always call into BuildSiteMap when these properties are accessed to ensure the provider is initialized and up to date. So, you can see then the recursion: BuildSiteMap requests ChildNodes which calls into BuildSiteMap.

The solution is to store the child/parent relationships in a separate look up mechanism (like a table) and reference it from there. I understand that it makes it much more complicated to work with this but that is part of the reason why the StaticSiteMapProvider and it's AddNode/RemoveNode/Clear methods are availible."


And now we know folks.



New Post Notification

Search Posts

Recent Posts


About Meeself
People call me Bobby DeRosa
I live somewhere in San Diego, CA
MCSD, MCAD, MCP

This theme was adapted from fUnique by fahlstad        Icons by FamFamFam        XHTML 1.0 Strict; tuned for Mozilla-powered browsers

Admin Login Administrator Login
Invalid login attempts are logged.
  Username:
  Password: