如何从xml文件中填充AspxMenu

时间:2010-08-11 13:22:21

标签: xml linq linq-to-xml asp.net-3.5 devexpress

在我的页面上,我有两个AspxMenu.Click 主菜单对应的值将显示在子菜单

XML语法:

<Demo>
<ClientCompanyId CompanyId="1">
<MyMenu>
<module Text="Basic Settings" ModID="Mod1" ModuleID="1" MenuType="0" Perm="False">
<menu Text="Forms" MID="1-1" ParentID="Mod1" MenuDescription="Mod" ModuleID="1" MenuType="0" Perm="False">
<Leaf Text="LookUp" MID="1-3" ParentID="1" MenuDescription="" ModuleID="1" MenuType="0" Perm="False" LeafNode="true" TargetUrl="" ModuleMenuID="1-3">
</Leaf>
<submenu Text="Bank Branch" MID="1-4" ParentID="1" MenuDescription="" ModuleID="1" MenuType="0" Perm="False">
<Leaf Text="BO Category" MID="1-5" ParentID="4" MenuDescription="" ModuleID="1" MenuType="0" Perm="False" LeafNode="true" TargetUrl="" ModuleMenuID="1-5">
</Leaf>
</submenu>
</menu>
<menu Text="Reports" MID="1-2" ParentID="Mod1" MenuDescription="Mod" ModuleID="1" MenuType="0" Perm="False">
<Leaf Text="Cheque Type" MID="1-6" ParentID="2" MenuDescription="" ModuleID="1" MenuType="0" Perm="False" LeafNode="true" TargetUrl="" ModuleMenuID="1-6">
</Leaf>
<Leaf Text="Stock Exchange" MID="1-7" ParentID="2" MenuDescription="" ModuleID="1" MenuType="0" Perm="False" LeafNode="true" TargetUrl="" ModuleMenuID="1-7">
</Leaf>
</menu>
</module>
</MyMenu>
</ClientCompanyId></Demo>

MasterMenu填写并点击活动:

 XDocument loaded = XDocument.Load(@"C:\Menu_Settings.xml");

            //XDocument loaded = XDocument.Load(@"C:\Menu_Settings.xml");

            var q = from c in loaded.Descendants("module")
                    where (int)c.Attribute("ModuleID") > 0
                    select new
                    {
                        Text = (string)c.Attribute("Text"),
                        ModID = (string)c.Attribute("ModID"),
                        ModuleID = (int)c.Attribute("ModuleID"),
                        MenuType = (int)c.Attribute("MenuType"),
                        Perm = (bool)c.Attribute("Perm")
                    };


            foreach (var name in q)
            {
                ASPxMasterMenu.RootItem.Items.Add(name.Text,name.ModuleID.ToString());


            } 

点击主菜单:

var r= ASPxMenu1.SelectedItem.Name;

            XDocument loaded = XDocument.Load(@"C:\Menu_Settings.xml");

            var q = from c in loaded.Descendants("menu")
                    where (int)c.Attribute("ModuleID") == Convert.ToInt32(ASPxMenu1.SelectedItem.Name)
                    select new
                    {
                        Text = (string)c.Attribute("Text"),
                        ModID = (string)c.Attribute("ModID"),
                        ModuleID = (int)c.Attribute("ModuleID"),
                        MenuType = (int)c.Attribute("MenuType"),
                        Perm = (bool)c.Attribute("Perm")
                    };


            foreach (var name in q)
            {
                AspxChildMenu.RootItem.Items.Add(name.Text);


            } 
模块标记菜单标记下的

包含多个 leaf和子菜单标记我要填充 AspxChildMenu 那些价值。如何?

1 个答案:

答案 0 :(得分:0)

试试这个

var menu = from c in loaded.Descendants("menu")
                     where c.Attribute("ModuleID").Value == "1"
                     select c;

        var q = from c in menu.Descendants()
                where c.Name == "Leaf" || c.Name == "submenu"
                select new
                {
                    Text = (string)c.Attribute("Text"),
                    ModID = (string)c.Attribute("ModID"),
                    ModuleID = (int)c.Attribute("ModuleID"),
                    MenuType = (int)c.Attribute("MenuType"),
                    Perm = (bool)c.Attribute("Perm")
                };