Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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#XML-存在多个同名元素的Linq查询_C#_Xml_Linq - Fatal编程技术网

C#XML-存在多个同名元素的Linq查询

C#XML-存在多个同名元素的Linq查询,c#,xml,linq,C#,Xml,Linq,当我的XML只有一个元素“HTTPSamplerProxy”时,我使用了下面的代码,现在我有多个同名的元素,我希望所有元素都存储起来 XElement HTTPSamplerProxy = doc.Descendants("HTTPSamplerProxy").FirstOrDefault(); path = (string)HTTPSamplerProxy.Elements("stringProp").Where(x => (string)x.Attribute("nam

当我的XML只有一个元素“HTTPSamplerProxy”时,我使用了下面的代码,现在我有多个同名的元素,我希望所有元素都存储起来

XElement HTTPSamplerProxy = doc.Descendants("HTTPSamplerProxy").FirstOrDefault();
        path = (string)HTTPSamplerProxy.Elements("stringProp").Where(x => (string)x.Attribute("name") == "HTTPSampler.path").FirstOrDefault();
        domain = (string)HTTPSamplerProxy.Elements("stringProp").Where(x => (string)x.Attribute("name") == "HTTPSampler.domain").FirstOrDefault();
        method = (string)HTTPSamplerProxy.Elements("stringProp").Where(x => (string)x.Attribute("name") == "HTTPSampler.method").FirstOrDefault();
您的方法已返回一个node
HTTPSamplerProxy
的集合

因此,只需消除
FirstOrDefault
,获取每个节点的集合和循环

IEnumerable<XElement> proxies = doc.Descendants("HTTPSamplerProxy");

foreach(var proxy in proxies)
{
}
IEnumerable proxy=doc.subjects(“HTTPSamplerProxy”);
foreach(代理中的var代理)
{
}
重复: