Hi,
Is there anything to consider when using XmlDocument function in unity3d?
I’m having this weird problem: When the function which uses XmlDocument is called from Awake() or OnGUI() the document is edited successfully. But when it’s called from inside a button event, event tough I get a well edited string before saving the document, it’s not able to modify the document itself.
Function that edits the file (sometimes):
public static void addTestProfile () {
string path = Application.dataPath + "/Documents/Profiles.xml";
Hashtable tempTable = new Hashtable();
tempTable.Add("user", "chuck II");
tempTable.Add("url", "funny");
tempTable.Add("passwrod", "1234asdf");
General.StoreProfile(tempTable, path);
}
public static void StoreProfile(Hashtable profile, string path) {
Debug.Log("profile to store name: " + profile["password"]);
XmlDocument doc = new XmlDocument();
doc.Load(profilesPath);
XmlElement element = doc.CreateElement("Profile");
XmlElement innerElement1 = doc.CreateElement("user");
innerElement1.InnerText = profile["user"] as string;
element.AppendChild(innerElement1);
XmlElement innerElement2 = doc.CreateElement("url");
innerElement2.InnerText = profile["url"] as string;
element.AppendChild(innerElement2);
XmlElement innerElement3 = doc.CreateElement("password");
innerElement3.InnerText = profile["password"] as string;
element.AppendChild(innerElement3);
doc.DocumentElement.AppendChild(element);
doc.Save(profilesPath);
Debug.Log(doc.InnerXml);
}
Here it works well and the file itself is edited:
void OnGUI () {
addTestProfile();
}
But some how this is not working:
// GUI Save btn
if (GUI.Button(new Rect(255, 20, 60, 35), "Add")) {
addTestProfile();
}
When I print the resultant string right before the save() xmlDocument function, it shows the new item but somehow the xml file remains the same.
Am I missing something important maybe related to the execution order? Something like timeout?
Thanks for your help.
Note: I’ve posted this question in stackoverflow but can’t find a fix