each xmlDocument has its own 'context'. you cannot add a node from
xmlDoc1 to xmlDoc2.
I needed it for small documents, so I created a xmlDoc3 and added to it
the nodes I needed from xmlDoc1 and xmlDoc2:
' --------------------------------------------
Public Shared Function mcXNodeListsAppend(ByVal xL1 As
XmlNodeList, ByVal xL2 As XmlNodeList) As System.Xml.XmlNodeList
' -----------------------------------------------
' Description: Append two XmlNodeLists into one list.
' I cannot simply append nodes, because of exception - "The node
to be inserted
' is from a different document context", So, I create a 3rd
list, and import
' each node from both original nodeLists to the 3rd one.
' ----------------------------------------------
'-- Create a new xmlDocument with one root node.
Dim tempDoc As New XmlDocument()
tempDoc.LoadXml(("<xxRoot></xxRoot>"))
Dim tempNode As XmlNode
Dim tempFragment As XmlDocumentFragment =
tempDoc.CreateDocumentFragment()
Dim maxI As Integer
Dim i As Integer
'-- Add childs to the root from both lists.
'-- Get xL1 elements:
If Not (xL1 Is Nothing) Then
maxI = xL1.Count
If (maxI > 0) Then
For i = 1 To maxI
tempNode = tempDoc.ImportNode(xL1.Item(i - 1), True)
tempDoc.DocumentElement.AppendChild(tempNode)
Next i
End If
End If
'-- Get xL2 elements:
If Not (xL2 Is Nothing) Then
maxI = xL2.Count
If (maxI > 0) Then
For i = 1 To maxI
tempNode = tempDoc.ImportNode(xL2.Item(i - 1), True)
tempDoc.DocumentElement.AppendChild(tempNode)
Next i
End If
End If
'-- Return the root ChildNodes.
Return tempDoc.FirstChild.ChildNodes
End Function