- Home /
Problems writing to existing xml
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!
Answer by zharik86 · Oct 11, 2014 at 07:39 AM
First, add some tag in your xml in question. Second, if you want to rewrite data in a XML file, the simplest way, it to create a text line, and then to save it in the file or somewhere else. I will write function of saving of your data:
public void myReSave() {
//create string as xml
string tempStr = "";
//Fill variable
tempStr = tempStr + "<levels>";
//Your all data level in dictionaries, change title of level 2 and save all dictionaries
(levels[1])["title"] = "MegaTitle Level 2"; //change title
for(int i = 0; i < levels.Count; i++) {
//Create one level data
tempStr = tempStr + "<level>";
tempStr = tempStr + "<name>" + (levels[i])["name"] + "</name>";
tempStr = tempStr + "<title>" + (levels[i])["title"] + "</title>";
tempStr = tempStr + "</level>";
}
tempStr = tempStr + "</levels>";
//And save your string into your file.
//Simple way, but remember, that filepath shall exist already, differently there will be an error
System.IO.File.WriteAllText("C:\yourfilepath\yourtextfile.xml", tempStr);
}
I hope that it will help you.
thanks for the reply zharik, but my xml is bigger than the example i posted here, and i want to know how to overwrite information using the xmlDocument procedure like in my Load function
@$$anonymous$$ad_Fox Are you want to get access to part of the file and to rewrite it?
@zharik86, i trya to load level info from the xml, put into variables and use ingame, that part works perfect, then, those variables are updated ingame, i try to put back those modified variables into their respective nodes in the xml document and then save the file. I dont know how to do that.
@$$anonymous$$ad_Fox Write your comments as comment, not answer(button "Add comment" below answer). For your case see this links: unitygems.com/xml and unitynoobs.blogspot.nl/2011/04/xml-writing-to-existing-xml-file.html?m=1.