开发者

LINQ to XML - Get element based on a nested elements value

开发者 https://www.devze.com 2023-01-19 23:03 出处:网络
I want to select an element within my XML based on the value of a nest element. Here is an example of the XML:

I want to select an element within my XML based on the value of a nest element.

Here is an example of the XML:

<Agents>
    <Agent ID="xxx">
        <Login>xxx</Login>
        <Password>xxxx</Password>
        <Products>
            <Product ID="zzz">
            </Product>
        </Products>
    </Agent>
</Agents>

Here is my first attempt at a LINQ query:

var DetailsOfUser开发者_高级运维Account =
  from agent in policySpecificationXml
        .Descendants("Agent")
        .FirstOrDefault(p => (string)p.Attribute("ID") == productId)
        .Descendants()
  select new

Thanks.


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消