VB.NET 将 xml 节点插入到现有的 XML 文档中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/687081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:24:31  来源:igfitidea点击:

VB.NET inserting xml nodes into an existing XML document

.netxmlvb.net

提问by Dan Williams

I'm simply trying to merge 2 xml documents (adding nodes from one into the other). I've done some Google searching, and tried a few things, but I always get the same error "The node to be inserted is from a different document context"

我只是想合并 2 个 xml 文档(将节点从一个添加到另一个)。我已经做了一些谷歌搜索,并尝试了一些东西,但我总是得到同样的错误“要插入的节点来自不同的文档上下文”

I'm sure I'm missing something simple, just seems like this should not be that difficult.

我确定我错过了一些简单的东西,只是看起来这不应该那么困难。

Here's my code:

这是我的代码:

    Dim xmlDoc482 As XmlDocument = New XmlDocument
    Dim xmlDoc486 As XmlDocument = New XmlDocument
    Dim xmlDoc490 As XmlDocument = New XmlDocument

    xmlDoc482.LoadXml(strSettlement482)
    xmlDoc486.LoadXml(strSettlement486)
    xmlDoc490.LoadXml(strSettlement490)

    Dim xmlSummarysNode490 As XmlNode = xmlDoc486("Summarys")
    Dim xmlSummaryNode482 As XmlNode = xmlDoc482("Summarys").LastChild
    Dim xmlSummaryNode486 As XmlNode = xmlDoc486("Summarys").LastChild

    Dim nodeDest As XmlNode
    nodeDest = xmlDoc490.ImportNode(xmlSummaryNode482, True)
    xmlSummarysNode490.AppendChild(nodeDest)

    nodeDest = xmlDoc490.ImportNode(xmlSummaryNode486, True)
    xmlSummarysNode490.AppendChild(nodeDest)

采纳答案by Dan Williams

This works great, other then my stupid, stupid typo

这很好用,除了我愚蠢的愚蠢的错字

This:

这个:

Dim xmlSummarysNode490 As XmlNode = xmlDoc486("Summarys")

Should be This:

应该是这个:

Dim xmlSummarysNode490 As XmlNode = xmlDoc490("Summarys")

An element/node must be added using the document you're adding it to.

必须使用您将其添加到的文档添加元素/节点。

回答by Mister Lucky

Try appending the imported nodes to the DocumentElement instead of the line Dim xmlSummarysNode490 As XmlNode = xmlDoc486("Summarys").

尝试将导入的节点附加到 DocumentElement 而不是 Dim xmlSummarysNode490 As XmlNode = xmlDoc486("Summarys") 行。

xmlDoc490.DocumentElement.AppendChild(nodeDest)

You could also try using the CloneNode()instead of ImportNode()before the insertion.

您也可以在插入之前尝试使用CloneNode()而不是ImportNode()

Finally something that helped me in merging in the past was to build a simple container xml then dump the children documents all into it.

最后,在过去帮助我合并的是构建一个简单的容器 xml,然后将子文档全部转储到其中。

xmlMerged.LoadXML("<set></set>")

So it becomes:

所以就变成了:

<set>
 <Summary>....</Summary>
 <Summary>....</Summary>
 ...
</set>

回答by Zach Johnson

You could create a helper function (or even better, an extension method) to create a copy of the XML node but changes the node's associated document to the document you want to merge into. You could also try using reflection, but that gets kind of messy...

您可以创建一个辅助函数(或者更好的扩展方法)来创建 XML 节点的副本,但将节点的关联文档更改为要合并到的文档。您也可以尝试使用反射,但这会变得有点混乱......

回答by Jim Counts

Here is an easy way to merge 2 xmls with the same schema:

这是合并 2 个具有相同架构的 xml 的简单方法:

Dim x1 As New Dataset
x1.ReadXml(path1)
Dim x2 As New Dataset
x2.ReadXml(path2)

x1.Merge(x2)
x1.WriteXml(path3)

You can probably adapt it to your own situation.

您可能可以根据自己的情况进行调整。