2016-08-18 60 views
0

我在下面的函數中收到此錯誤。要插入的節點來自不同的文檔上下文

Public Function GetXmlEnvironment(ByVal xmlTree As XmlNode, ByVal objUser As XTUser, ByVal objModule As XTModuleInfo, ByVal objProject As XTProject, ByVal objPage As IXTPage) As XmlNode 

    Dim objXml As XmlDocument = New XmlDocument() 
    Dim xmlEnvironment As XmlElement 

    xmlEnvironment = objXml.CreateElement("Environment") 
    If (xmlTree.HasChildNodes()) Then 
     xmlTree.FirstChild.AppendChild(xmlEnvironment) ' Error occurs here 
    End If 
' ... 
End Function 

我是否需要將XmlElement轉換爲XmlNode?

+2

使用xmlTree.FirstChild的OwnerDocument創建一個新元素,而不是全新的XmlDocument。 – Will

+0

@Parfait,標題是錯誤。 –

+0

我打算在添加節點之前先說'導入'節點... – Codexer

回答

1
Public Function GetXmlEnvironment(ByVal xmlTree As XmlNode, ByVal objUser As XTUser, ByVal objModule As XTModuleInfo, ByVal objProject As XTProject, ByVal objPage As IXTPage) As XmlNode 
    Dim objXml As XmlDocument = New XmlDocument() 
    Dim xmlEnvironment As XmlElement 
    xmlEnvironment = objXml.CreateElement("Environment") 
    If (xmlTree.HasChildNodes()) Then 
    xmlTree.FirstChild.AppendChild(xmlTree.OwnerDocument.ImportNode(xmlEnvironment, True)) 
    End If 
    '...' 
End Function 
相關問題