469,578 Members | 1,799 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

indenting dropDownList items when pulling from XML

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
Nov 18 '05 #1
2 2025
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.

Nov 18 '05 #2
> 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
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Daniel Doyle | last post: by
10 posts views Thread by ads | last post: by
11 posts views Thread by Santosh | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.