indenting dropDownList items when pulling from XML

Discussion in 'ASP .Net' started by Darrel, Oct 30, 2004.

  1. Darrel

    Darrel Guest

    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
     
    Darrel, Oct 30, 2004
    #1
    1. Advertising

  2. Darrel

    jongalloway Guest

    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.
     
    jongalloway, Oct 31, 2004
    #2
    1. Advertising

  3. Darrel

    Darrel Guest

    > 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
     
    Darrel, Oct 31, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RAB
    Replies:
    2
    Views:
    383
    Chris R. Timmons
    Nov 6, 2005
  2. Yama

    Indenting INPUT

    Yama, Nov 12, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    384
    Daniel Fisher\(lennybacon\)
    Nov 14, 2005
  3. John

    Indenting with XHTML

    John, Sep 9, 2004, in forum: HTML
    Replies:
    29
    Views:
    1,175
  4. Hyunchan Kim
    Replies:
    1
    Views:
    722
    Martin Honnen
    Dec 18, 2003
  5. Harrie
    Replies:
    6
    Views:
    893
    Manuel Collado
    Mar 1, 2006
Loading...

Share This Page