将 Xelement 添加为没有父级的 Xelement 的同级
我正在尝试弄清楚这是否可能。基本上,我需要创建一个 XElement,然后向该 XElement 添加一个或多个同级,但没有父级。稍后我会将该 XElement 列表添加到 Parent,但在此之前需要在构建此 XElement 列表和其他 XElement 列表时具有一定的灵活性。这可能吗?
所以我会有类似的东西:
Public items As XElement = <ItemA>Something</ItemA>
然后我需要添加一个元素,以便结果看起来像:
<ItemA>Something</ItemA>
<ItemB>Something Else</ItemB>
该结果是我需要作为单个对象传递的结果。我已经搞乱了 IEnumerable(Of XElement),但没有 .Add。
有什么想法吗?
I'm trying figure out if this is possible. Basically, I need to create an XElement and then add one or more siblings to that XElement, but without a parent. I'll add that list of XElements to a Parent later, but need some flexibility in building this and other lists of XElements before doing so. Is this possible?
So I would have something like:
Public items As XElement = <ItemA>Something</ItemA>
And then I need to add an element so that the result looks like:
<ItemA>Something</ItemA>
<ItemB>Something Else</ItemB>
That result is what I need to pass around as a single object. I've messed arounnd with IEnumerable(Of XElement), but there is no .Add.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是行不通的,元素只是兄弟元素,因为它们共享一个父元素...
您必须使用稍后更改或替换的临时父元素。
当然,您可以使用任何集合 (
List
) 来保存一个列表,稍后将其转换为同级列表。不清楚你的问题是什么。That's not going to work, elements are only siblings because they share a Parent ...
You'll have to use a temporary parent that you later change or replace.
You can of course use any collection (
List<XElement>
) to keep a list that you later turn into a list of siblings. Not clear what your problem with that is.