Error: "The Node to Be Inserted Is from a Different Document Context"

Error: The node to be inserted is from a different document context

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;

The node to be inserted is from a different document context

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

ArgumentException: The node to be inserted is from a different document context

Using Xml Linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
@"<ROOT>
<NOT_THIS_ONE>
</NOT_THIS_ONE>

<THIS_ONE>
</THIS_ONE>
</ROOT>";

XDocument doc = XDocument.Parse(xml);

XElement thisOne = doc.Descendants("THIS_ONE").FirstOrDefault();

thisOne.Add(new XElement("CHILD", new XElement("POINTS", 102)));
doc.Save("Assets/Resources/GamePoints.xml");
}
}
}

Import node to another XML document

Assuming your new.xml starts out something like this:

<ConfigExport>
<SaleItems>
</SaleItems>
</ConfigExport>

You seem to appending the node back into the same document you read it from in the first place with the next to last line - I assume that's not what you intended. Ambiguously defined variable names makes the code hard to read and debug.

Here's a simple re-write with clearer naming:

$importXml = [xml]( Get-Content "C:\IT\HowardCo\Compare\New.xml" )
$exportXml = [xml]( Get-Content "C:\IT\HowardCo\Compare\ConfigExport.xml" )
$node = $exportXml.SelectSingleNode( "//SaleItem[SaleItemId=2]" )
$newNode = $importXml.ImportNode($node, $true)
$importXml.DocumentElement.AppendChild($newnode)
$importXml.Save( "C:\IT\HowardCo\Compare\New.xml" )

In your original code, with naming like this, you might have more quickly seen that you were appending the $newNode into $exportXml.

Create xmlDocument from another document

You need to use the Import Node method to import the XmlNode from the first document into the context of the second:

objNewsDoc.LoadXml(strNewsDetail);       // Current XML
XmlDocument docRss = new XmlDocument(); // new Xml Object i Want to create

XmlElement news = docRss.CreateElement("news"); // creating the wrapper news node
//Import the node into the context of the new document. NB the second argument = true imports all children of the node, too
XmlNode importNewsItem = docRss.ImportNode(objNewsDoc.SelectSingleNode("newsItem"), true);
news.AppendChild(importNewsItem);

EDIT

You are very close to your answer, the main issue you have now is that you need to append your news element to your main document. I would recommend doing the following if you want your output document to look like this:

<news>
<newsItem>...</newsItem>
<newsItem>...</newsItem>
</news>

Rather than create a new XmlElement, news, instead, when you create docRSS, do the following:

XmlDocument docRss = new XmlDocument();
docRss.LoadXml("<news/>");

You now have an XmlDocument that looks like this:

<news/>

Then, rather than news.AppendChild, simply:

docRSS.DocumentElement.AppendChild(importNewsItem);

This appends each newsItem under the news element (which in this instance is the document element).

Unable to use InsertAfter for XmlDocument in c#

Your tempNode is from xdoc document context. You should import it to xmlMaster document context:

XmlNode importedECU = xmlMaster.ImportNode(tempNode, true);

Also instead of InsertAfter it's better to use AppendChild and append new ECU nodes as children of master ver element:

var masterVer = masterRoot.SelectSingleNode("//ver");

foreach(var file in files)
{
var xdoc = new XmlDocument();
xdoc.Load(file);
var tempNode = xdoc.DocumentElement.LastChild.LastChild;
var importedECU = xmlMaster.ImportNode(tempNode, true);
masterVer.AppendChild(importedECU);
}


Related Topics



Leave a reply



Submit