I’m trying to convert a System.Web.SiteMapNode to my own SiteMapNode class (see below):
public class SiteMapNode {
public virtual SiteMapNode Parent { get; set; }
public virtual string Title { get; set; }
public virtual IList<SiteMapNode> Children { get; set; }
public SiteMapNode(System.Web.SiteMapNode node) {
Parent = node.ParentNode != null ? new SiteMapNode(node.ParentNode) : null;
Title = node.Title;
Children = node.ChildNodes.Cast<System.Web.SiteMapNode>().Select(n => new SiteMapNode(n)).ToList();
}
}
Now i was hoping i could say:
var node = new MyApp.SiteMapNode(System.Web.CurrentNode);
The trouble now though is that the constructor sets the Children and then for each child it creates an instance of my SiteMapNode class. Now this will then set the parent which sets the children… You can probably see the problem now, this causes an infinite loop.
I can’t quite see how to resolve this. I’d appreciate any advice. Thanks
You can make a list of nodes where you will store all the nodes.
First you create all the nodes without relation references and populate the list, and after that, you call populate children/parent method for every node, in which you will look for the needed nodes to reference it in the node.
The list is also useful for search purposes.