XNode和XElement之间没有隐式转换

问题描述

| 我有两个变量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”将使您能够抓住整个车辆节点并忽略属于它的所有其他节点,因为我不认为您希望重复这些节点。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...