-1

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!

Dark Kool
  • 11
  • 1
  • 7

1 Answers1

0

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");
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Thank you for the comment! I'm gonna try it right now and be back with you in a second! – Dark Kool Dec 21 '20 at 09:37
  • Hey! So, this really did the trick! The only problem was "POINTS" isn't a child of "CHILD" but I believe I need to create the "CHILD" XElement and then child.AddChild("POINTS", 102); And, finally thisOne.AddChild(child) and save. Gonna try it really fast and update this! Once again, thank you very much! – Dark Kool Dec 21 '20 at 10:06
  • Yap, it needs to be done like I previously mentioned (I think, there might be other way). But, without a doubt, you helped me a ton! Thank you very very much! – Dark Kool Dec 21 '20 at 10:10