XNode 和 XElement 之间没有隐式转换

发布于 2024-11-09 22:36:43 字数 953 浏览 2 评论 0原文

我有两个 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.IEnumerableSystem.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花之痕靓丽 2024-11-16 22:36:43

在三元中,您需要决定是使用Nodes() 还是Descendants()。你不能两者兼得。 Nodes() 返回 IEnumerableDescendants() 返回 IEnumerable。三元表达式需要返回相同的类型。

更改:

XTemp.Descendants(xmlns + "Vehicles").Nodes()

为:

XTemp.Descendants(xmlns + "Vehicles").Nodes() 

或者您可以将 Nodes() 添加到第二个表达式。

编辑:如果我正确理解您的评论,您想要选择每个车辆的节点及其本身。尝试用此方法代替 Descendants(xmlns + "Vehicle")

.Descendants(xmlns + "Vehicle")
.SelectMany(d => d.DescendantNodesAndSelf().Take(1))

Take(1) 将允许您抓取整个车辆节点并忽略所属的所有其他节点因为我认为您不希望重复这些内容。

In the ternary you need to decide whether to use Nodes() or Descendants(). You can't have both. Nodes() returns an IEnumerable<XNode>, and Descendants() returns IEnumerable<XElement>. The ternary expressions need to return the same type.

Change:

XTemp.Descendants(xmlns + "Vehicles").Nodes()

to:

XTemp.Descendants(xmlns + "Vehicles").Nodes() 

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"):

.Descendants(xmlns + "Vehicle")
.SelectMany(d => d.DescendantNodesAndSelf().Take(1))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文