【发布时间】:2013-02-04 11:11:55
【问题描述】:
我有以下 xml:
<?xml version="1.0" encoding="utf-8" ?>
<layout>
<menu name="Employees" url="Employees.aspx" admin="0">
</menu>
<menu name="Projects" url="Projects.aspx" admin="1">
</menu>
<menu name="Cases" url="Cases.aspx" admin="1">
</menu>
<menu name="CaseView" url="CaseView.aspx" admin="1" hidden="1" parent="Projects">
</menu>
<menu name="Management" url="" admin="1">
<item name="Groups" url="Groups.aspx" admin="1" parent="Management"/>
<item name="Statuses" url="Statuses.aspx" admin="1"/>
</menu>
</layout>
在这里,我有 CaseView 和 Groups,它们都具有“父”属性。
目前我这样迭代:
IEnumerable<XElement> menus =
doc.Element("layout").Elements();
foreach (var menu in menus)
{
string name = menu.Attribute("name").Value;
string active = "";
string url = menu.Attribute("url").Value;
if(activePage == url)
{
active = "class=\"active\"";
}
...
我想要的是:
if(activePage == url || ActiveIsChildOf(name, activePage))
{
active = "class=\"active\"";
}
本质上该方法需要查找是否存在以activePage作为其url属性的元素。如果有,看是否有父属性;如果是,请检查父级是否 == 名称。
有什么方法可以按属性或其他方式查找元素吗? 例如:
XElement e = doc.GetByAttribute("url",activePage)
谢谢
【问题讨论】: