合并两个 XElement
我不太确定如何问这个问题,或者这是否存在,但我需要合并两个 XElement,其中一个优先于另一个,以成为一个元素。
这里首选 VB.NET 和 Linq,但是任何语言如果能够演示如何执行此操作,而无需我编写代码来手动分离和解析每个元素和属性,那么任何语言都会有所帮助。
例如,假设我有两个元素。让我对他们的不同之处感到幽默。
1.
<HockeyPlayer height="6.0" hand="left">
<Position>Center</Position>
<Idol>Gordie Howe</Idol>
</HockeyPlayer>
2.
<HockeyPlayer height="5.9" startinglineup="yes">
<Idol confirmed="yes">Wayne Gretzky</Idol>
</HockeyPlayer>
合并的结果将是
<HockeyPlayer height="6.0" hand="left" startinglineup="yes">
<Position>Center</Position>
<Idol confirmed="yes">Gordie Howe</Idol>
</HockeyPlayer>
注意以下几点:#1 的 height
属性值覆盖了 #2。 hand
属性和值只是从#1 复制而来(它在#2 中不存在)。 #2 中的 startinglineup
属性和值已被复制(#1 中不存在)。 #1 中的 Position
元素被复制(它在 #2 中不存在)。 #1 中的 Idol
元素值覆盖 #2,但 #2 的 confirmed
属性(在 #1 中不存在)被复制。
Net net,如果存在冲突(意味着两者具有相同的元素和/或属性),则 #1 优先于 #2;如果不存在冲突,则它们都复制到最终结果。
我尝试过搜索此内容,但似乎找不到任何内容,可能是因为我用来搜索的词太通用了。有什么想法或解决方案(尤其是 Linq)吗?
I'm not quite sure how to ask this, or if this even exists, but I have a need to merge two XElements with one taking precendence over the other, to become just one element.
The preference here is VB.NET and Linq, but any language would be helpful if it demonstrates how to do this without me coding to manually pick apart and and resolve every single element and attribute.
For example, let's say I have two elements. Humor me on them being as different as they are.
1.
<HockeyPlayer height="6.0" hand="left">
<Position>Center</Position>
<Idol>Gordie Howe</Idol>
</HockeyPlayer>
2.
<HockeyPlayer height="5.9" startinglineup="yes">
<Idol confirmed="yes">Wayne Gretzky</Idol>
</HockeyPlayer>
The result of a merge would be
<HockeyPlayer height="6.0" hand="left" startinglineup="yes">
<Position>Center</Position>
<Idol confirmed="yes">Gordie Howe</Idol>
</HockeyPlayer>
Notice a few things: the height
attribute value of #1 overrode #2. The hand
attribute and value was simply copied over from #1 (it doesn't exist in #2). The startinglineup
attribute and value from #2 was copied over (it doesn't exist in #1). The Position
element in #1 was copied over (it doesn't exist in #2). The Idol
element value in #1 overrides #2, but #2's attribute of confirmed
(it doesn't exist in #1) is copied over.
Net net, #1 takes precendence over #2 where there is a conflict (meaning both have the same elements and/or attributes) and where there is no conflict, they both copy to the final result.
I've tried searching on this, but just can't seem to find anything, possibly because the words I'm using to search are too generic. Any thoughts or solutions (esp. for Linq)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了其他人寻找同样的东西,因为我认为贡献的人早已失去了兴趣......我需要做一些类似的事情,但更完整一点。但仍然不完全完整,正如 XMLDoc 所说,它不能很好地处理非元素内容,但我不需要这样做,因为我的非元素内容要么是文本,要么不重要。请随意增强和重新发布...
哦,它是 C# 4.0,因为这就是我使用的......
For the sake of others looking for the same thing, as I assume both the people contributing have long since lost interest... I needed to do something similar but a little more complete. Still not totally complete though, as the XMLDoc says it does not handle non-element content well, but I don't need to as my non-element content is either text or unimportant. Feel free to enhance and re-post...
Oh and it's C# 4.0 as that's what I use...
这是一个控制台应用程序,可生成您的问题中列出的结果。它使用递归来处理每个子元素。它不检查的一件事是出现在
Elem2
中但不在Elem1
中的子元素,但希望这将帮助您开始寻找解决方案。我不确定我是否会说这是最好的解决方案,但它确实有效。
编辑:我刚刚注意到该函数缺少
As XElement
。事实上,我很惊讶它没有它就能工作!我每天都使用 VB.NET,但它有一些我仍然不完全理解的怪癖。Here's a console app that produces the result listed in your question. It uses recursion to process each sub element. The one thing it doesn't check for is child elements that appear in
Elem2
that aren't inElem1
, but hopefully this will get you started towards a solution.I'm not sure if I would say this is the best possible solution, but it does work.
Edit: I just noticed that the function was missing
As XElement
. I'm actually surprised that it worked without that! I work with VB.NET every day, but it has some quirks that I still don't totally understand.