xml - 通过 LinqQuery 获取对象
问题描述
我有这个 XML 代码,我想过滤掉特定机构中的人员。
</persons>
<person>
<name>
<fn>Donald Duck</fn>
<n>
<family>Duck</family>
<given>Donald</given>
</n>
</name>
<email />
<tel teltype="voice" />
<tel teltype="mobile" />
<adr>
</adr>
<institutionrole roletype="Employee" />
<extension>
<institutions>
<institution institution="Division1">
</institution>
<institution institution="Division2">
</institution>
</institutions>
</extension>
</person>
</persons>
我想要机构 Division2 中的所有人员。
使用此代码,我可以获得所有员工,但未按机构过滤。
var users = (from person in xmlDoc.Descendants("person")
where (person.Element("institutionrole").Attribute("roletype").Value.ToLower() == "employee"
解决方案
您可以使用以下内容根据嵌套元素进行过滤;这将过滤角色类型为“员工”的人员,以及他们是否属于给定机构。
var users = from person in xmlDoc.Descendants("person")
where person.Element("institutionrole").Attribute("roletype").Value.ToLower() == "employee"
&& person.Descendants("institutions").Elements("institution").Attributes("institution").Where(ins=>ins.Value.ToLower() == "division2").Any()
select person;
推荐阅读
- javascript - jquery按钮点击检测
- python - 当我在服务器中配置 Django 和 Nginx 时,为什么几个小时后失去 mysql 连接?
- r - 将单个变量(即向量)通过管道传输到 ggplot?
- python - 情绪 = model.predict(twt,batch_size=1,verbose = 2)[0] 是什么意思?
- php - Laravel 邮件提供发件人详细信息
- sql - 在 Oracle SQL 或 PL/SQL 中根据名称和计数合并相似的行
- actions-on-google - 带有日期/时间比较的新 Google Action
- ruby-on-rails - Rails RoutingError 来自约束失败(IP 将文件列入白名单)
- php - WordPress 自定义帖子类型:如何在标签档案中显示 (tag.php)
- revit-api - 如何在 Revit API 中找到 Ramp 着陆空间和中间着陆?