- Home /
Save and Load data from Local XML file won't work
I'm building my first prototype for multi-game sharing achievement system. It means there's two or more games using one shared achievement data. I'm storing the data in an .xml file, for now the xml only contains coins data.
So i want my program to load and modify the amount of coins from it.
This is My Xml :
<Achievement><Coins>800</Coins></Achievement>
My Code :
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
public class XMLTest : MonoBehaviour {
string Filelocation, FileName;
private Text CoinsText;
public int Coins;
public int Price;
void Start () {
CoinsText = GameObject.Find("CoinsText").GetComponent<Text>();
Filelocation = "E:/Projek/Projek Pak Suprapedi/XML Test/";
FileName = "Coins.xml";
}
public void WriteToXML()
{
XmlDocument XmlDoc = new XmlDocument();
if(File.Exists(Filelocation + FileName))
{
XmlDoc.Load(Path.Combine(Filelocation, FileName));
XmlNodeList AchievementList = XmlDoc.GetElementsByTagName("Achievement");
foreach(XmlNode node in AchievementList)
{
if(node.Name == "Coins")
{
node.InnerText = Coins.ToString();
}
}
XmlDoc.Save(Path.Combine(Filelocation, FileName));
}
}
public void LoadFromXML()
{
XmlDocument XmlDoc = new XmlDocument();
if(File.Exists(Filelocation + FileName))
{
XmlDoc.Load(Path.Combine(Filelocation,FileName));
XmlNodeList AchievementList = XmlDoc.GetElementsByTagName("Achievement");
foreach(XmlNode node in AchievementList)
{
if(node.Name == "Coins")
{
Coins = int.Parse(node.InnerText);
}
}
}
}
public void Buy()
{
Coins -= Price;
}
public void GetMoney()
{
Coins += Price;
}
void Update()
{
CoinsText.text = "Your Coins : " + Coins.ToString();
}
}
I'm assigning the public void to a button so i can modify the coins then save or load it. But it seems that load and save function won't do anything, nothing happen to xml file.
I will deploy my game to Android platform, but I don't know if this method would work, would it ?
In the code you provided there is no call to either WriteToX$$anonymous$$L() or LoadFromX$$anonymous$$L(). Show us how you call these methods.
@kmgr I'm using button for each public function, there's 4 button, one for save, load, decrease, and increase money
Answer by Risal Fajar · Aug 11, 2015 at 09:25 PM
I will answer my own question. I've found the problem. so the problem is :
XmlNodeList AchievementList = XmlDoc.GetElementsByTagName("Achievement");
I'm replacing it with
XmlNodeList AchievementList = XmlDoc.GetElementsByTagName("Coins");
And it works !!
I think you can understand what is the problem now
@Risal Fajar i tried your solution for xml and it works perfectly on editor and pc (thank you by the way). but couldn't find a way to work on Android. $$anonymous$$ay you tell me how can it work on $$anonymous$$obile too (android and ios)
Your answer
Follow this Question
Related Questions
SaveLoad Profile c# to xml 4 Answers
Save and Load from XML help 1 Answer
Loading Xml file issues 0 Answers
Problems with saving/loading score with PlayerPrefs [C#] 1 Answer
Filestream/SaveManager 2 Answers