Hi! i followed some tutorials regarding XML files and im having my issues writing to an existing xml, my xml is something like this (real one have more items)
<?xml version="1.0" encoding="utf-8"?>
<levels>
<name>Level 1-1</name>
<title>This is level 1</title>
</level>
<name>Level 1-2</name>
<title>This is level 2</title>
</level>
</levels>
I’ve a script to load this xml and put it’s data to some variables so i can use ingame, this is the script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
public class LoadXmlData : MonoBehaviour // the Class
{
public int actualLevel = 1;
static int LevelMaxNumber;
static int WaipointCounter = 0;
public static string lvlname = "";
public static string lvltitle = "";
public TextAsset XMLData;
List<Dictionary<string,string>> levels = new List<Dictionary<string,string>>();
Dictionary<string,string> obj;
void Start()
{ //Timeline of the Level creator
GetLevel();
StartCoroutine(LevelLoadInfo(0.0F));
LevelMaxNumber = levels.Count;
}
public void GetLevel()
{
XmlDocument xmlDoc = new XmlDocument(); // xmlDoc is the new xml document.
xmlDoc.LoadXml(XMLData.text); // load the file.
XmlNodeList levelsList = xmlDoc.GetElementsByTagName("level"); // array of the level nodes.
foreach (XmlNode levelInfo in levelsList)
{
XmlNodeList levelcontent = levelInfo.ChildNodes;
obj = new Dictionary<string,string>(); // Create a object(Dictionary) to colect the both nodes inside the level node and then put into levels[] array.
foreach (XmlNode levelsItens in levelcontent) // levels itens nodes.
{
if(levelsItens.Name == "name")
{
obj.Add("name",levelsItens.InnerText); // put this in the dictionary.
}
if(levelsItens.Name == "title")
{
obj.Add("title",levelsItens.InnerText); // put this in the dictionary.
}
}
levels.Add(obj); // add whole obj dictionary in the levels[].
}
}
IEnumerator LevelLoadInfo(float Wait)
{
levels[actualLevel-1].TryGetValue("name",out lvlname);
levels[actualLevel-1].TryGetValue("title",out lvltitle);
yield return new WaitForSeconds(Wait);
}
void Update()
{
}
}
ok, everything works fine, but i would like to know how i could change some data and put it back to the xml file and save it, for example, how to change the level 2 title, i tried a lot of different things but none work at all, can i access the node directly?, do i have to loop every node and find the one a need like in the load script? how is the way to do it? thanks in advance!