如何用递归算法根据XML生产菜单
这是没有用递归算法生产的菜单 但速度很慢
var xe = XElement.Load("Menu.xml").Elements("RibbonPage");
RibbonPageGroup[] rpg;
BarButtonItem[] bbbi;
RibbonPage[] rp;
rp = new RibbonPage[xe.Count()];
for (int i = 0; i < xe.Count(); i++)
{
rpg = new RibbonPageGroup[xe.ElementAt(i).Elements("RibbonPageGroup").Count()];
for (int k = 0; k < xe.ElementAt(i).Elements("RibbonPageGroup").Count(); k++)
{
bbbi = new BarButtonItem[xe.ElementAt(i).Elements("RibbonPageGroup").ElementAt(k).Elements("BarButtonItem").Count()];
for (int j = 0; j < xe.ElementAt(i).Elements("RibbonPageGroup").ElementAt(k).Elements("BarButtonItem").Count(); j++)
{
bbbi[j] = new BarButtonItem();
bbbi[j].Name = xe.ElementAt(i).Elements("RibbonPageGroup").ElementAt(k).Elements("BarButtonItem").ElementAt(j).Attribute("Name").Value;
bbbi[j].Caption = xe.ElementAt(i).Elements("RibbonPageGroup").ElementAt(k).Elements("BarButtonItem").ElementAt(j).Attribute("Caption").Value;
bbbi[j].ItemClick += new ItemClickEventHandler(BarButtonItem_ItemClick);
}
rpg[k] = new RibbonPageGroup();
rpg[k].Name = xe.ElementAt(i).Elements("RibbonPageGroup").ElementAt(k).Attribute("Name").Value;
rpg[k].Text = xe.ElementAt(i).Elements("RibbonPageGroup").ElementAt(k).Attribute("Text").Value;
rpg[k].ShowCaptionButton = false;
rpg[k].ItemLinks.AddRange(bbbi);
}
rp[i] = new RibbonPage();
rp[i].Name = xe.ElementAt(i).Attribute("Name").Value;
rp[i].Text = xe.ElementAt(i).Attribute("Text").Value;
rp[i].MergedGroups.AddRange(rpg);
}
}
ribbon.Pages.AddRange(rp);
<?xml version="1.0" encoding="utf-8" ?>
<Ribbon>
<RibbonPage Name="rpJC" Text="基础资料">
<RibbonPageGroup Name="rpg1" Text="品种管理">
<BarButtonItem Name="品种分类维护" Caption="品种分类维护" ItemClick="品种分类维护_ItemClick"></BarButtonItem>
<BarButtonItem Name="品种资料管理" Caption="品种资料管理" ItemClick="品种资料管理_ItemClick"></BarButtonItem>
<BarButtonItem Name="品种资料审核" Caption="品种资料审核" ItemClick="品种资料审核_ItemClick"></BarButtonItem>
</RibbonPageGroup>
<RibbonPageGroup Name="rpg2" Text="客户管理">
<BarButtonItem Name="客户分类维护" Caption="客户分类维护" ItemClick="客户分类维护_ItemClick"></BarButtonItem>
<BarButtonItem Name="客户资料管理" Caption="客户资料管理" ItemClick="客户资料管理_ItemClick"></BarButtonItem>
<BarButtonItem Name="客户资料审核" Caption="客户资料审核" ItemClick="客户资料审核_ItemClick"></BarButtonItem>
</RibbonPageGroup>
<RibbonPageGroup Name="rpg3" Text="职员管理">
<BarButtonItem Name="职员信息管理" Caption="职员信息管理" ItemClick="职员信息管理_ItemClick"></BarButtonItem>
<BarButtonItem Name="职员信息审核" Caption="职员信息审核" ItemClick="职员信息审核_ItemClick"></BarButtonItem>
</RibbonPageGroup>
<RibbonPageGroup Name="rpg4" Text="地区管理">
<BarButtonItem Name="地区资料管理" Caption="地区资料管理" ItemClick="地区资料管理_ItemClick"></BarButtonItem>
</RibbonPageGroup>
<RibbonPageGroup Name="rpg5" Text="仓库管理">
<BarButtonItem Name="仓库信息管理" Caption="仓库信息管理" ItemClick="仓库信息管理_ItemClick"></BarButtonItem>
</RibbonPageGroup>
<RibbonPageGroup Name="rpg6" Text="医院管理">
<BarButtonItem Name="医院科室管理" Caption="医院科室管理" ItemClick="医院科室管理_ItemClick"></BarButtonItem>
</RibbonPageGroup>
<RibbonPageGroup Name="rpg7" Text="供货人管理">
<BarButtonItem Name="供货人信息管理" Caption="供货人信息管理" ItemClick="供货人信息管理_ItemClick"></BarButtonItem>
</RibbonPageGroup>
<RibbonPageGroup Name="rpg8" Text="组织机构管理">
<BarButtonItem Name="组织机构维护" Caption="组织机构维护" ItemClick="组织机构维护_ItemClick"></BarButtonItem>
</RibbonPageGroup>
</RibbonPage>
<RibbonPage Name="rpZC" Text="政策管理">
<RibbonPageGroup Name="rpg1" Text="部门管理">
<BarButtonItem Name="部门地区分管管理" Caption="部门地区分管管理" ItemClick="部门地区分管管理_ItemClick"></BarButtonItem>
<BarButtonItem Name="部门品种分类管理" Caption="部门品种分类管理" ItemClick="部门品种分类管理_ItemClick"></BarButtonItem>
<BarButtonItem Name="部门客户分类管理" Caption="部门客户分类管理" ItemClick="部门客户分类管理_ItemClick"></BarButtonItem>
</RibbonPageGroup>
<RibbonPageGroup Name="rpg2" Text="员工分管">
<BarButtonItem Name="员工销售分管" Caption="员工销售分管" ItemClick="员工销售分管_ItemClick"></BarButtonItem>
<BarButtonItem Name="员工部门分管管理" Caption="员工部门分管管理" ItemClick="员工部门分管管理_ItemClick"></BarButtonItem>
<BarButtonItem Name="员工推广分管管理" Caption="员工推广分管管理" ItemClick="员工推广分管管理_ItemClick"></BarButtonItem>
</RibbonPageGroup>
<RibbonPageGroup Name="rpg3" Text="费用政策">
<BarButtonItem Name="固定费用定额设置" Caption="固定费用定额设置" ItemClick="固定费用定额设置_ItemClick"></BarButtonItem>
<BarButtonItem Name="推广费用通用结算政策" Caption="推广费用通用结算政策" ItemClick="推广费用通用结算政策_ItemClick"></BarButtonItem>
<BarButtonItem Name="推广费用特殊结算政策" Caption="推广费用特殊结算政策" ItemClick="推广费用特殊结算政策_ItemClick"></BarButtonItem>
</RibbonPageGroup>
<RibbonPageGroup Name="rpg4" Text="库存政策">
<BarButtonItem Name="公司库存预警设置" Caption="公司库存预警设置" ItemClick="公司库存预警设置_ItemClick"></BarButtonItem>
</RibbonPageGroup>
<RibbonPageGroup Name="rpg5" Text="供货人分管">
<BarButtonItem Name="供货人分管管理" Caption="供货人分管管理" ItemClick="供货人分管管理_ItemClick"></BarButtonItem>
</RibbonPageGroup>
<RibbonPageGroup Name="rpg6" Text="货位分管">
<BarButtonItem Name="员工货位分管" Caption="员工货位分管" ItemClick="员工货位分管_ItemClick"></BarButtonItem>
</RibbonPageGroup>
</RibbonPage>
<Ribbon>
</xml>
LinqtoXML不好么?
参考:
var xmlData = XElement.Load("Silverlight_Class.xml");
var data = LoadRec(this.SlClassData);
private IEnumerable<SlClassInfo> LoadRec(XElement section)
{
return (from xElem in section.Elements()
select new SlClassInfo
{
Name = xElem.get_Name().ToString(),
IsAbstract = ((xElem.Attribute("abstract") != null) && (xElem.Attribute("abstract").get_Value() == "True")) ? true : false,
Child = this.LoadRec(xElem)
});
}
用递归解决
/// <summary>
/// 绑定树
/// </summary>
private void BindDataToTree()
{
System.Xml.XmlDocument document = new System.Xml.XmlDataDocument();
document.Load(Server.MapPath("MenuData.xml"));
TreeHtml = CreateTreeHtml(document.DocumentElement, 0);
}
/// <summary>
/// 创建栏目树
/// </summary>
/// <param name="document">xml文档</param>
/// <param name="deep">树深度</param>
/// <returns></returns>
private string CreateTreeHtml(System.Xml.XmlNode document, int deep)
{
StringBuilder treeHtml = new StringBuilder();
foreach (System.Xml.XmlNode node in document.ChildNodes)
{
int chlidNodesCount = node.ChildNodes.Count;//子节点数
string showName = GetAttributesValue(node.Attributes["Name"]);//显示栏目名
.......//生成树的代码
if (chlidNodesCount > 0)
{
treeNodeHtml = CreateTreeHtml(node, deep + 1);
}
if (treeNodeHtml.Length > 0)
{
treeHtml.Append(treeNodeHtml);
}
}
}
/// <summary>
/// 取得节点值
/// </summary>
/// <returns></returns>
private string GetAttributesValue(XmlAttribute attributeValue)
{
if (attributeValue == null)
{
return "";
}
else
{
return attributeValue.Value.Trim();
}
}