?
Solved

How to recursively get parent nodes until top of tree is reached?

Posted on 2005-03-25
1
Medium Priority
?
270 Views
Last Modified: 2010-05-18
I have the following XML as example:


<Menu Name="home" Text="Home" NavigateUrl="/tmc" Visible="True" >
     <Menu Name="sites" Text="Control Sites" NavigateUrl="/tmc/sites.aspx" PageTitle="Monitor Control Sites" Visible="True">          
          <Menu Name="br" Text="Brisbane" NavigateUrl="locations.aspx?site=br" Visible="True">

                             ....this can keep going 'n' levels deeper
          </Menu>
    </Menu>
</Menu>

Now, if I was in the deepest child node in the tree (for example sake), how can I recursively navigate to each parent node (ie parent of current, parent of current, parent of current and so on).

Cheers.


0
Comment
Question by:smacca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 23

Accepted Solution

by:
b1xml2 earned 2000 total points
ID: 13635975
using System.Xml;

public sealed class NodeIterator
{
      private NodeIterator() {}
            
      public static XmlNode [] GetNodesArray(XmlNode node)
      {
            ArrayList al = new ArrayList();
            al.Add(node);
            while (node != null)
            {
                  if (node.ParentNode != null && node.ParentNode is XmlElement)
                  {
                              al.Add(node.ParentNode);
                              node = node.ParentNode;
                  }
                  else
                  {
                              node = null;
                  }
                  
                  
            }
            (XmlNode [])return al.ToArray(typeof(XmlNode));
      }

}


///calling it.
XmlNode [] nodes = NodeIterator.GetNodesArray(doc.SelectSingleNode("//Menu[@NavigateUrl='locations.aspx?site=br']"));
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question