根据子元素的数量对 SitemapNodes 进行排序
在应用程序中,我正在读取站点地图文件,将其添加到 SiteMapNodeCollection 中,迭代它并根据某些条件呈现结果。
但在传递这些 SiteMapNodeCollection
进行操作之前,我需要根据子节点的数量对节点进行排序(包括是否有子子节点)。我想过使用扩展方法,但对使用相同的方法感到困惑,因为它是一个层次结构集合。
SiteMapDataSourceView siteMapView = (SiteMapDataSourceView)siteMapData.GetView(string.Empty);
//Get the SiteMapNodeCollection from the SiteMapDataSourceView
SiteMapNodeCollection nodes = (SiteMapNodeCollection)siteMapView.Select(DataSourceSelectArguments.Empty);
***//Need to sort the nodes based on the number of child nodes it has over here.***
//Recursing through the SiteMapNodeCollection...
foreach (SiteMapNode node in nodes)
{
//rendering based on condition.
}
可以给我前进的指导。
In an application i am reading from sitemap file adding it on to a SiteMapNodeCollection
Iterating through it and rendering the results based on some conditions.
But before passing on these SiteMapNodeCollection
for the manipulation i need to sort the nodes based on the number of childnodes(including if any sub child nodes).I thought of using Extension methods but am confused on using the same,as it is a hierarchial collection.
SiteMapDataSourceView siteMapView = (SiteMapDataSourceView)siteMapData.GetView(string.Empty);
//Get the SiteMapNodeCollection from the SiteMapDataSourceView
SiteMapNodeCollection nodes = (SiteMapNodeCollection)siteMapView.Select(DataSourceSelectArguments.Empty);
***//Need to sort the nodes based on the number of child nodes it has over here.***
//Recursing through the SiteMapNodeCollection...
foreach (SiteMapNode node in nodes)
{
//rendering based on condition.
}
could give me guidance on moving ahead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以编写一个方法来计算指定节点的子节点计数,如下所示:
然后按如下方式使用它:
You could write a method which calculates a subnodes count for the node specified like this:
and then use it like this: