XNode 和 XElement 之间没有隐式转换
我有两个 XElement 类型的变量 XResult、Xtemp。
我试图从 Xtemp 中提取所有
元素,并将它们添加到
下的 Xresult 中。
似乎在 Xtemp 中有时
会出现在
下,有时它会单独出现。
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")));
在上面的代码中,我使用三元运算符来检查
是否有子元素,然后获取所有子元素,否则获取所有
元素。
这会产生错误:System.Collections.Generic.IEnumerable
和 System.Collections.Generic.IEnumerable
有人可以帮助我纠正这个问题吗? 提前致谢。 BB。
I have two variables XResult, Xtemp of type XElement.
I am trying to extract all the <vehicle>
elements from Xtemp and add them to Xresult under <vehicles>
.
It seems that in Xtemp sometimes <vehicle>
will appear under <vehicles>
, and sometimes it will be by itself.
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")));
In the code above I am using the ternary operator to check if <vehicles>
has childs then get all of them else go get all <vehicle>
elements.
This produces the error: no implict conversion between System.Collections.Generic.IEnumerable<System.Xml.Linq.XNode>
and System.Collections.Generic.IEnumerable <System.Xml.Linq.XElement>
Can some body help me correct this.
Thanks in advance.
BB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在三元中,您需要决定是使用
Nodes()
还是Descendants()
。你不能两者兼得。Nodes()
返回IEnumerable
,Descendants()
返回IEnumerable
。三元表达式需要返回相同的类型。更改:
为:
或者您可以将
Nodes()
添加到第二个表达式。编辑:如果我正确理解您的评论,您想要选择每个车辆的节点及其本身。尝试用此方法代替
Descendants(xmlns + "Vehicle")
:Take(1)
将允许您抓取整个车辆节点并忽略所属的所有其他节点因为我认为您不希望重复这些内容。In the ternary you need to decide whether to use
Nodes()
orDescendants()
. You can't have both.Nodes()
returns anIEnumerable<XNode>
, andDescendants()
returnsIEnumerable<XElement>
. The ternary expressions need to return the same type.Change:
to:
Or you could add
Nodes()
to the second expression.EDIT: if I understood your comment correctly you want to select each vehicle's nodes and itself. Try this in place of
Descendants(xmlns + "Vehicle")
:The
Take(1)
will allow you to grab the entire vehicle node and ignore all the other nodes that belong to it since I don't think you wanted those being repeated.