我试图查询包含WCF条目的web.Config
文件。linq to xml获取所有子节点
在<service>
节点有一个name attribute
,我试图匹配。到目前为止,我的代码在进行匹配时工作正常,但我的问题是它只返回<endpoint>
节点中的1个。
例如,我可以有这个片段的xml:
<service name="a">
<endpoint>1</endpoint>
<endpoint>2</endpoint>
<endpoint>3</endpoint>
</service>
<service name="b">
<endpoint>1</endpoint>
<endpoint>2</endpoint>
</service>
每次我得到一个比赛,我希望它显示所有的那场比赛的<endpoint>
子节点。
这是我到目前为止的代码:
IEnumerable<XElement> xmlURL =
from el in xmlFile.Root.Descendants("service")
where (string)el.Attribute("name") == serviceString
select el.Element("endpoint");
Console.WriteLine("Start: " + serviceString);
foreach (XElement el in xmlURL)
{
Console.WriteLine(el);
}
Console.WriteLine("End: " + serviceString + "\n\n");
目前,当它的比赛只有1个端点所示。
在这种情况下,当只有一个孩子的级别,后代会没事的。否则,您可能想要使用仅返回直接子项的元素(XName)。 –
@JoachimIsaksson好点。 – CodingGorilla
当我尝试使用后代时出现以下错误: 无法将类型'System.Collections.Generic.IEnumerable>'隐式转换为' System.Collections.Generic.IEnumerable ”。存在明确的转换(您是否缺少演员?) –
webdad3