LINQ to XML-基于嵌套元素值获取元素 [英] LINQ to XML - Get element based on a nested elements value
本文介绍了LINQ to XML-基于嵌套元素值获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想基于nest元素的值在XML中选择一个元素.
I want to select an element within my XML based on the value of a nest element.
以下是XML的示例:
<Agents>
<Agent ID="xxx">
<Login>xxx</Login>
<Password>xxxx</Password>
<Products>
<Product ID="zzz">
</Product>
</Products>
</Agent>
</Agents>
这是我第一次尝试LINQ查询:
Here is my first attempt at a LINQ query:
var DetailsOfUserAccount =
from agent in policySpecificationXml
.Descendants("Agent")
.FirstOrDefault(p => (string)p.Attribute("ID") == productId)
.Descendants()
select new
谢谢.
推荐答案
尚不完全清楚,但听起来像是您想要的东西...
Not entirely clear, but sounds like you want something like...
var detailsOfUserAccount = policySpecificationXml
.Descendants("Agent")
.Where(agent => agent.Descandants("Product")
.Any(product => (string)product.Attribute("ID")
== productId))
.FirstOrDefault();
这篇关于LINQ to XML-基于嵌套元素值获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文