.net Linq到XML选择祖先多达2个级别
根据下面的XML,我需要重新选择positions.position.manager\u position与position参数匹配的员工 如何使用linq到XML实现这一点.net Linq到XML选择祖先多达2个级别,.net,linq,linq-to-xml,.net,Linq,Linq To Xml,根据下面的XML,我需要重新选择positions.position.manager\u position与position参数匹配的员工 如何使用linq到XML实现这一点 <employee id="0004000"> <!-- ... --> </employee> <employee id="0004001"> <username>Administrator</username> <
<employee id="0004000">
<!-- ... -->
</employee>
<employee id="0004001">
<username>Administrator</username>
<positions>
<position id="00008001" isPrimary="1">
<title>GENERAL MANAGER</title>
<manager_position>00008431</manager_position>
</position>
</positions>
</employee>
<employee id="0004002">
<!-- ... -->
</employee>
管理员
总经理
00008431
您可以这样做:
employees.Where(e => e.Element("positions")
.Elements("position")
.Elements("manager_position")
.Any(mp => mp.Value == position))
选择那些至少有一个
manager\u position
元素与您要查找的职位匹配的员工。此示例将返回匹配的员工,或者在未找到任何员工时返回null:
var employees = XElement.Parse(
"<employees><employee><!-- ... --></employee></employees>");
var results = employees
.Elements("employee")
.Where(e => e.Descendants("manager_position").Value == "00008431")
.SingleOrDefault();
var employees=XElement.Parse(
"");
var结果=员工
.要素(“员工”)
其中(e=>e.1(“经理职位”)。值==“00008431”)
.SingleOrDefault();
另请参见: