递归方法如下,是把List中的项递归生成XML结构,递归函数本身没有问题,但生成的XML元素都闭合着,没形成结构啊?why?
private XmlElement LoopListToXml(List<RoleItem> all, RoleItem curItem, ref XmlElement node) {
var subItems = all.Where(ee => ee.NodeID == curItem.ID).ToList(); //子节点
foreach (RoleItem cur in subItems) {
if (cur.ModuleType == ModuleTypeEnum.Menu) {
XmlElement elementItem = CreateXML_Menu(cur);
node.AppendChild(elementItem);
}
if (cur.ModuleType == ModuleTypeEnum.MenuItem) {
XmlElement elementItem = CreateXML_Item(cur);
node.AppendChild(elementItem);
}
if (cur.ID != 0){
LoopToMenuXml(all, cur, ref node);
}
}
return node;
}
生成的结构如下,每一行后面都闭合了:
<Menus>
<Menu Text="Menu_0" ShortKey="" ToolButton="false" ButtonIcon="" />
<Menu Text="Menu_1" ShortKey="" ToolButton="false" ButtonIcon="" />
<Item Type="MenuItem" Text="MenuItem_1" ToolButton="false" ButtonIcon=""/>
<Item Type="MenuItem" Text="MenuItem_1" ToolButton="false" ButtonIcon="" />
<Item Type="MenuItem" Text="MenuItem_17" ToolButton="false" ButtonIcon="" />
<Item Type="MenuItem" Text="MenuItem_17" ToolButton="false" ButtonIcon="" />
<Item Type="MenuItem" Text="MenuItem_17" ToolButton="false" ButtonIcon="" />
<Menu Text="Menu_2" ShortKey="" ToolButton="false" ButtonIcon="" />
<Menu Text="Menu_3" ShortKey="" ToolButton="false" ButtonIcon="" />
<Menus>
已解决
带入的元素错了,递归全都Append到了第一次传入的node