recursion - Generate an algorithm in C# to take hierarchy data to generate this html -


As a reference I have this html and I have a set of hierarchical data

< / P>

; & Lt; Li & gt; & Lt; Input type = "checkbox" & gt; & Lt; Label & gt; Node 1 & lt; / Label & gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; Input type = "checkbox" & gt; & Lt; Label & gt; Node 1.1 & lt; / Label & gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; Input type = "checkbox" & gt; & Lt; Label & gt; Node 1.1.1 & lt; / Labels & gt; & Lt; / Ul & gt; & Lt; / Ul & gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; Input type = "checkbox" & gt; & Lt; Label & gt; Node 1.2 & lt; / Label & gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; Input type = "checkbox" & gt; & Lt; Label & gt; Node 1.2.1 & lt; / Label & gt; & Lt; Li & gt; & Lt; Input type = "checkbox" & gt; & Lt; Label & gt; Node 1.2.2 & lt; / Label & gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; Input type = "checkbox" & gt; & Lt; Label & gt; Node 1.2.3.1 & lt; / Labels & gt; & Lt; Li & gt; & Lt; Input type = "checkbox" & gt; & Lt; Label & gt; Node 1.2.3.2 & lt; / Labels & gt; & Lt; / Ul & gt; & Lt; Li & gt; & Lt; Input type = "checkbox" & gt; & Lt; Label & gt; Node 1.2.4 & lt; / Label & gt; & Lt; Li & gt; & Lt; Input type = "checkbox" & gt; & Lt; Label & gt; Node 1.2.5 & lt; / Label & gt; & Lt; Li & gt; & Lt; Input type = "checkbox" & gt; & Lt; Label & gt; Node 1.2.6 & lt; / Label & gt; & Lt; / Ul & gt; & Lt; / Ul & gt; & Lt; Li & gt; & Lt; Input type = "checkbox" & gt; & Lt; Label & gt; Node 2 & lt; / Label & gt;

Now to generate this HTML output, to get this sort of data, now the C # code is required. I have an node object Who has one Children () Property is what I have done here, but I have some code in this code like & lt; / Ul & gt; is unavailable.

Note: _b is a string builder object on which the end of a string output is being used.

  Fixed public Zero GenerateTree (node ​​node, bool skipUL) {if (node.Children.Count ()> gt;) {bool included = false; Exotic (node ​​child.Node node child) {if (! And skipUL!) {_b.AppendLine (" ul>"); Included = true; } Skipull = false; _b.AppendLine ("& lt; li> Input type = 'checkbox' & gt;" + childNode .name); GenerateTree (childNode, skipUL); } If (included) {_b.AppendLine (""); }}}}    

I will take a different child approach, i.e. the beginning of the collection One unit (when it's likely that your root will be a collection, unless always is not going to only be a root element).

It is being said: public static string dump (list & lt; node & ng; nodes) {if (nodes == faucet} nodes number == 0) string return. Empty; Stringsbilder SB = new string builder ("& lt; ul>"); Foreign (var n in nodes) {sb.AppendFormat ("

  • {0}", n.Name); Sb.Append (dump (n.children)); Sb.Append ("& lt; / li>"); } Sb.Append (""); Return sb.ToString (); }

    And here's a demo with which I expect to expect your objects:

    < / Div>

  • Comments