尝试这个:
private static HtmlGenericControl GenerateList(int count)
{
HtmlGenericControl parent = new HtmlGenericControl("ul");
for (int i = 1; i <= count; i++)
{
HtmlGenericControl ChildLi = new HtmlGenericControl("li");
ChildLi.InnerText = i.ToString();
HtmlGenericControl ChildUl = new HtmlGenericControl("ul");
ChildLi.Controls.Add(ChildUl);
for (int j = 1; j <= i; j++) {
HtmlGenericControl FinalLi = new HtmlGenericControl("li");
FinalLi.InnerText = j.ToString();
ChildUl.Controls.Add(FinalLi);
}
parent.Controls.Add(ChildLi);
}
return parent;
}
编辑:
您应该将输入表示为树结构。在这里,我使用了一个Node
具有ID
和Children
属性的对象:
class Node
{
public int ID { get; set; }
public Node[] Children { get; set; }
public Node(int id, Node[] children)
{
ID = id;
Children = children;
}
public Node(int id)
{
ID = id;
Children = new Node[] { };
}
}
class Program
{
public static HtmlGenericControl GetList(Node root)
{
HtmlGenericControl ul = new HtmlGenericControl("ul");
GetListImpl(root, ul);
return ul;
}
private static void GetListImpl(Node root, HtmlGenericControl rootElement)
{
foreach (var item in root.Children)
{
HtmlGenericControl li = new HtmlGenericControl("li");
li.InnerText = item.ID.ToString();
if (item.Children.Length > 0)
li.Controls.Add(GetList(item));
rootElement.Controls.Add(li);
}
}
public static string HtmlGenericControlToString(HtmlGenericControl control)
{
string contents = null;
using (System.IO.StringWriter swriter = new System.IO.StringWriter())
{
HtmlTextWriter writer = new HtmlTextWriter(swriter);
control.RenderControl(writer);
contents = swriter.ToString();
}
return contents;
}
static void Main(string[] args)
{
Node root = new Node(0,
new Node[]{
new Node(1, new Node[]{
new Node(1),
new Node(2)
}),
new Node(2, new Node[]{
new Node(1),
new Node(2, new Node[]{
new Node(1)
})
})
});
var ul = GetList(root);
var html = HtmlGenericControlToString(ul);
Console.WriteLine(html);
}
}