问题描述
|
我有两个变量XResult,即XElement类型的Xtemp。
我试图从Xtemp中提取所有the0ѭ元素,并将它们添加到res1ѭ下的Xresult中。
似乎在Xtemp中,有时ѭ0会出现在
<vehicles>
之下,有时它本身会出现。
XResult.Descendants(xmlns + \"Vehicles\").FirstOrDefault().Add(
XTemp.Descendants(xmlns + \"Vehicles\").Nodes().Count() > 0
? XTemp.Descendants(xmlns + \"Vehicles\").Nodes()
: (XTemp.Descendants(xmlns + \"SearchDataset\").FirstOrDefault().Descendants(xmlns + \"Vehicle\")));
在上面的代码中,我使用三元运算符检查<vehicles>
是否有孩子,然后获取所有孩子,否则获取所有<vehicle>
元素。
产生错误::7ѭ和System.Collections.Generic.IEnumerable <System.Xml.Linq.XElement>
之间没有隐式转换
可以帮我纠正这个问题吗?
提前致谢。
BB。
解决方法
在三进制中,您需要决定使用
Nodes()
还是Descendants()
。不能两者兼有。 Nodes()
返回IEnumerable<XNode>
,Descendants()
返回IEnumerable<XElement>
。三元表达式需要返回相同的类型。
更改:
XTemp.Descendants(xmlns + \"Vehicles\").Nodes()
至:
XTemp.Descendants(xmlns + \"Vehicles\").Nodes()
或者,您可以在第二个表达式中添加Nodes()
。
编辑:如果我正确理解了您的评论,则需要选择每个车辆的节点及其本身。试试这个代替Descendants(xmlns + \"Vehicle\")
:
.Descendants(xmlns + \"Vehicle\")
.SelectMany(d => d.DescendantNodesAndSelf().Take(1))
“ѭ20”将使您能够抓住整个车辆节点并忽略属于它的所有其他节点,因为我不认为您希望重复这些节点。