Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
C# LINQ to XML-基于嵌套元素值获取元素_C#_Linq - Fatal编程技术网

C# LINQ to XML-基于嵌套元素值获取元素

C# LINQ to XML-基于嵌套元素值获取元素,c#,linq,C#,Linq,我想根据嵌套元素的值在XML中选择一个元素 以下是XML的一个示例: <Agents> <Agent ID="xxx"> <Login>xxx</Login> <Password>xxxx</Password> <Products> <Product ID="zzz"> </Product&g

我想根据嵌套元素的值在XML中选择一个元素

以下是XML的一个示例:

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

谢谢。

不太清楚,但听起来你想要的是

var detailsOfUserAccount = policySpecificationXml
    .Descendants("Agent")
    .Where(agent => agent.Descandants("Product")
                         .Any(product => (string)product.Attribute("ID")
                                             == productId))
    .FirstOrDefault();

你的问题能说得更清楚些吗?你期望的结果是什么?如果您只是第一次尝试而没有说明您的尝试有什么问题,我们如何帮助您?对不起,mastoj。基本上,如果我有多个代理节点,我希望只使用要搜索的产品ID来选择包含产品ID的产品节点的代理。
var detailsOfUserAccount = policySpecificationXml
    .Descendants("Agent")
    .Where(agent => agent.Descandants("Product")
                         .Any(product => (string)product.Attribute("ID")
                                             == productId))
    .FirstOrDefault();