Answer the question
In order to leave comments, you need to log in
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>
<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
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");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question