A
A
Andrew2010-12-28 13:20:20
ASP.NET
Andrew, 2010-12-28 13:20:20

Asp.net and trees?

Hello. I'm learning a little ASP.NET, and I can't figure out how to display a tree structure from xml with unknown nesting. (Perhaps with the help of TreeView)
xslt is probably not suitable here, because I have a checkbox for each item, on which I need to hang an event in .net
That is, I have an xml like this:

<root>
    <item name="test1">
          <item name="test11">
               <item name="test111" />
               <item name="test112" />
          </item>
          <item name="test12" />
    </item>
    <item name="test2" />
</root>

And it should be output like this:
<ul>
   <li>
      <input type="checkbox">
      <p>test1</p>
      <ul>
          <li>
             <input type="checkbox">
             <p>test11</p>
             <ul>
                ....
             </ul>
          </li>
      <ul>
   </li>
  <li>
     <input type="checkbox">
     <p>test2</p>
  </li>
</ul>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2010-12-28
@Morfi

So you can just recursively output everything. Approximately like this:

var str = @"<root>
    <item name=""test1"">
        <item name=""test11"">
           <item name=""test111"" />
           <item name=""test112"" />
        </item>
        <item name=""test12"" />
    </item>
    <item name=""test2"" />
  </root>";
  
  
  var rootNode = XElement.Parse(str);
  
  OutNode(rootNode);


void OutNode(XElement rootNode)
{
    Console.WriteLine ("ul");
    foreach(var node in rootNode.Elements().Where(x => x.Name=="item") )
  {
     Console.WriteLine ("li");
      Console.WriteLine (node.Attribute("name").Value);
      if(node.HasElements)   OutNode(node);
    Console.WriteLine ("/li");
  }
   Console.WriteLine ("/ul");
}


Well, arrange it directly in the template, the helper method, or whatever is convenient.

M
maseal, 2010-12-28
@maseal

And you can also write a small xslt-shku and "twirl" with such a transformation as you like, without changing the program code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question