indenting dropDownList items when pulling from XML

D

Darrel

I have the hierarchical menu structure for my site stored in an XML file.

I have a page where a person can select from a list of all pages from a
dropDownList. I'd like the list to echo the visual hierarchy of the menu.
So, I'd like it to look like this:

Home
- Secondary
- Secondary
- - Tertiary
- Secondary

I can easily read the XML file and populate the dropDownList. Is there a
practical way to add the indent? Is there a way to read the 'node depth' of
each record pulled in from an XML file?

-Darrel
 
J

jongalloway

Darrel -
You can either iterate the nodes and keep the indent index in a
variable, iterate with some fixed level loops, or you can do this with
XSLT.

For the XSLT approach, here's a good example which maps hierarchial xml
to unordered lists:
http://www.4guysfromrolla.com/webtech/042303-1.shtml

For the iteration with the indent index, you'd have a function you
could call recursively that takes an XmlNode and an indent level int
variable. It would iterate through the node's children and call itself
with indent + 1:

Pseudocode:
private void iterateNodes(XmlNode node, int depth)
foreach(XmlNode child in node.Children)
{
//do something with current node
Response.Write("level: " + depth.ToString() + "<br>value: " +
child.value + "<br><br>";

//iterate children
iterateNodes(child, depth + 1);
}

For the fixed level iteration approach, you'd just want to loop through
all the top level nodes. Inside that, you'd loop through the secondary
nodes, and inside that you'd loop through the tertiary nodes. Inside
each of those loops, you'd know which level you were at so you'd know
how to indent.
 
D

Darrel

Darrel -
You can either iterate the nodes and keep the indent index in a
variable, iterate with some fixed level loops, or you can do this with
XSLT.

For the XSLT approach, here's a good example which maps hierarchial xml
to unordered lists:
http://www.4guysfromrolla.com/webtech/042303-1.shtml

Right...but I'm trying to attach it to a asp:dropdownlist. Can my codebehind
read the result of an XSLT transformation directly? Right now I'm just
reading the XML into the dataset.

As for iterating through the XML...well, that just seems wrong...isn't the
whole point of XML that you don't need the recursive calls to evaluate
hierarchy? I'm sure that method is perhaps the way to go, it just seems to
defeat the purpose of the XML to begin with.

-Darrel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top