Recursive method Everyone should be very familiar with, in short, the method internally called itself, so that repeated repeated repetition of the execution,
But to worry about the cycle of death yo ...
When our system needs to dynamically generate menus, which means that our system's menus are in the database, the database structure is similar to the following:
id,name,parent_id This design approach, we need to dynamically generate menus based on the relationship between ID and parent_id.
As for how to generate the menu, today is not introduced, today is mainly about the use of recursive way to generate XML structure of the document.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data;
6 using System.Collections;
7 using System.Xml;
8
9 using Nunit.framework;
10
Namespace H.mysystem.test
12 {
[Testfixture]
Class MyTest
15 {
Public DataTable inittable ()
17 {
DataTable dt = new DataTable ();
19
DT. Columns.Add ("ID", typeof (String));
DT. Columns.Add ("Name", typeof (String));
DT. Columns.Add ("parent_id", typeof (String));
23
The DataRow row1 = dt. NewRow ();
row1["ID"] = "1";
row1["Name"] = "First node";
27
DataRow ROW11 = dt. NewRow ();
row11["ID"] = "11";
row11["Name"] = "4th node";
row11["parent_id"] = "2";
32
Row2 DataRow =<= "" dd= "" class= "" style= "Color:rgb (0, 0, 0);" >
C # recursive generation of XML