Search code examples
c#xmlargumentexception

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


I've searched Stackoverflow on this question, as well as other forums but no one seems to be making this the way I'm making. By this I mean, in my code, instead of using XMLNode, I'm using XMLElement.

So, without further ado, my intention is to save in an already existing XML Document, a new Element is a child of other existing Elements besides the root.

This is an example on my XML File:

<ROOT>
  <NOT_THIS_ONE>
  </NOT_THIS_ONE>

  <THIS_ONE>
  </THIS_ONE>
</ROOT>

So, this is my code:

//XML File
TextAsset repository = Resources.Load("Repository") as TextAsset;

//Create XML Reference
XmlDocument xmlDocument = new XmlDocument();

//Load XML File into XML Reference
xmlDocument.LoadXml(repository.text);

//Root Node
XmlNode statsNode = GetRootNode();

//Get History Node
XmlNode thisOneNode = statsNode.ChildNodes.Item(1);

The GetRootNode() function is this:

//Create Xml Reference
XmlDocument xmlData = new XmlDocument();

//Load Xml File into Xml Reference
xmlData.LoadXml(repository.text);

//Get Root Node
return xmlData.ChildNodes.Item(1);

The thisOneNode gets the <THIS_ONE> Element as a Node (at least that's what I think it does). Later on, I do this:

XmlElement childOfThisOne = xmlDocument.CreateElement("CHILD");

XmlElement pointsSession = xmlDocument.CreateElement("POINTS");
pointsSession.InnerText = points.ToString();

childOfThisOne.AppendChild(pointsSession);

thisOneNode.AppendChild(childOfThisOne);

xmlDocument.Save("Assets/Resources/GamePoints.xml");

And my intention with this would be something like:

<ROOT>
  <NOT_THIS_ONE>
  </NOT_THIS_ONE>

  <THIS_ONE>
    <CHILD>
      <POINTS>102</POINTS>
    </CHILD>
  </THIS_ONE>
</ROOT>

But I get the error in the title: "ArgumentException: The node to be inserted is from a different document context."

And the line in question is this: thisOneNode.AppendChild(childOfThisOne);

Now, where I've searched and the articles I found, people were using XmlNode and even used an xmlDocument.ImportNode(); I tried that too and the same error occurred. Now, I don't how to fix this and I'm requesting your help on this one.

Thank you for your time and happy holidays!


Solution

  • 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");
            }
        }
    }