C#操作XML之读取数据

时间:2024-01-08 15:58:08
List<Ztree> ZTreeList = new List<Ztree>();
XDocument MenuConfigDoc = XDocument.Load("Config/MenuConfig.xml");
//按文档顺序返回此文档或元素的经过筛选的子代元素集合。 集合中只包括具有匹配 System.Xml.Linq.XName 的元素。
var MenuGroups = MenuConfigDoc.Descendants("MenuGroups");
foreach (var menuGroups in MenuGroups)
{
Ztree menuGroupsZtree = new Ztree();
menuGroupsZtree.id = menuGroups.Attribute("id").Value;
menuGroupsZtree.name = menuGroups.Attribute("name").Value;
menuGroupsZtree.pId = "";
var MenuGroup = menuGroups.Elements("MenuGroup");
foreach (var menuGroup in MenuGroup)
{
Ztree menuGroupZtree = new Ztree();
menuGroupZtree.id = menuGroup.Attribute("id").Value;
menuGroupZtree.name = menuGroup.Attribute("name").Value;
menuGroupZtree.pId = menuGroups.Attribute("id").Value;
ZTreeList.Add(menuGroupZtree);
var Menu = menuGroup.Elements("Menu");
foreach (var menu in Menu)
{
Ztree menuEasyZtree = new Ztree();
menuEasyZtree.id = menu.Attribute("id").Value;
menuEasyZtree.name = menu.Attribute("name").Value;
menuEasyZtree.pId = menuGroup.Attribute("id").Value;
ZTreeList.Add(menuEasyZtree);
}
}
ZTreeList.Add(menuGroupsZtree);
}
Console.ReadKey();