误差:"要插入的节点是从一个不同的文档上下文" [英] Error: "The node to be inserted is from a different document context"
本文介绍了误差:"要插入的节点是从一个不同的文档上下文"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我打电话 XmlNode.AppendChild()
,我得到这个错误:
When I am calling XmlNode.AppendChild()
, I get this error:
要插入的节点是从一个不同的文档上下文
The node to be inserted is from a different document context.
这code是由这是这个(请忽略可选参数,我在C#版本为他们过载)的VB 6.0版本转换
This code was converted from its VB 6.0 version which was this (please ignore the optional parameters, I have overloads for them in C# version):
Public Function XMLNewChildNode(ByVal oParent As IXMLDOMNode, ByVal _
sName As String, Optional ByVal sNamespaceURI As String = "", _
Optional ByVal sNodeType As String = "element") As IXMLDOMNode
'**************** DESCRIPTION *******************
'Create a new Child Node for passed Parent.
'***************** VARIABLES ********************
Dim oNode As IXMLDOMNode
'************************************************
Set oNode = moDoc.createNode(sNodeType, sName, sNamespaceURI)
Call oParent.appendChild(oNode)
Set XMLNewChildNode = oNode
End Function
为什么VB code的工作,而C#不?是否有VB和C#如何处理XML之间的差异,我需要知道的?
Why does the VB code work while the C# does not? Are there differences between how VB and C# handle XML, that I need to be aware of?
推荐答案
您需要将节点导入到文档中追加前:
You need to import the node into the document before appending it:
XmlNode oNode = moDoc.CreateNode(sNodeType, sName, sNamespaceURI);
//necessary for crossing XmlDocument contexts
XmlNode importNode = oParent.OwnerDocument.ImportNode(oNode, true);
oParent.AppendChild(importNode);
return oNode;
这篇关于误差:"要插入的节点是从一个不同的文档上下文"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文