C # recursively generate XML,
Recursive methods should be familiar to everyone. In short, they are called internally by the method itself, so that repeated and repeated executions are repeated,
But worry about endless loops...
When the menu needs to be dynamically generated by our system, that is to say, the menu of our system exists in the database, and the database structure is similar:
In the design of ID, Name, Parent_ID, We need to dynamically generate menus based on the relationship between ID and Parent_ID ..
As for how to generate menus, we will not introduce them today. Today we will mainly introduce documents that use recursive methods to generate XML structures ..
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
11 namespace H. MySystem. Test
12 {
13 [TestFixture]
14 class MyTest
15 {
16 public DataTable InitTable ()
17 {
18 DataTable dt = new DataTable ();
19
20 dt. Columns. Add ("ID", typeof (string ));
21 dt. Columns. Add ("Name", typeof (string ));
22 dt. Columns. Add ("Parent_ID", typeof (string ));
23
24 DataRow row1 = dt. NewRow ();
25 row1 ["ID"] = "1 ";
26 row1 ["Name"] = "first node ";
27
28 DataRow row11 = dt. NewRow ();
29 row11 ["ID"] = "11 ";
30 row11 ["Name"] = "4th nodes ";
31 row11 ["Parent_ID"] = "2 ";
32
33 DataRow row2 = <= "" dd = "" class = "" style = "color: rgb (0, 0, 0);">