Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net Linq到XML选择祖先多达2个级别_.net_Linq_Linq To Xml - Fatal编程技术网

.net Linq到XML选择祖先多达2个级别

.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> <

根据下面的XML,我需要重新选择positions.position.manager\u position与position参数匹配的员工

如何使用linq到XML实现这一点

<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();
另请参见: