XmlDocument problem, modifies the file only sometimes

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

Hi, welcome to the forum!

Generally, you shouldn’t use OnGUI to make cumulative changes or call functions with side effects. A common way around the problem is to set boolean variables in OnGUI and then look for them in Update:-

var buttonWasClicked: boolean;

function OnGUI() {
  if (GUI.Button(...)) {
    buttonWasClicked = true;
    // Other code without cumulative changes.
  }
}


function Update() {
  if (buttonWasClicked) {
    buttonWasClicked = false;
    // Handle the button action.
  }
}

Thanks Andeee,
Unfortunately it’s still failing … a colleague of mine says it might be a c# issue like not getting references to the xml library on time. Maybe it’s better to switch to javascript, also by using sqlite I can avoid this odd.