xml writer not actually saving until I open monodevelop

I’ve got an editor script which saves data from my level to an xml file whenever I save the scene in unity. The problem is that this only works if I then open or switch focus to another program, then come back to Unity. Even if I open other scenes first, the file will update with the correct data as soon as I switch programs then switch back but not before.

Any ideas?

I have seen exactly the same happening on my XML files.
Also heard from a friend of mine he was experiencing maya files not reloading properly.
This is under Unity4.0 for us though.

Interesting problem… Here are some thoughts…

What technique are you using to write the file?

Create file and open for writing.
Write data.
Close file.

Or are you appending to the file or using an existing file?

What are you doing with the file handle after you’ve closed the file? Is it a local or global variable?

If you switch out of Unity and kill Unity, is the data saved?

Here’s the actual code. I’m opening, reading, altering and then saving an existing file.

string fileName = AssetDatabase.GetAssetPath(levelInfo.GetComponent<LevelInfoScript>().contractLevelInfo);
		
		//Now we integrate with the Xml doc
		XmlDocument xmlDoc = new XmlDocument();
		//grab the doc from the Level Info object
		xmlDoc.Load(fileName);
		//grab all the levels from the doc
		XmlNodeList levelList = xmlDoc.GetElementsByTagName("level");
		//grab the base element
		XmlElement elmRoot = xmlDoc.DocumentElement;
		
		//If we're trying to save a file that hasn't been added to the doc yet then we need to add it.
		if(levelNumber >= levelList.Count){
			//Step through all the levels to your current level and if it hasn't been added to the list then add temp ones until you reach the current one
			for(int i = 0; i < levelNumber + 1; i++){
				if(i >= levelList.Count){
					//Create a clone of the first node
					XmlNode baseNode = elmRoot.FirstChild.Clone ();
					//Then append that clone
					elmRoot.AppendChild(baseNode);
				}
			}
			//Now re-get the list of levels
			levelList = xmlDoc.GetElementsByTagName("level");
			
		}
		
		//grab the current node
		XmlNode infoNode = levelList[levelNumber];
		//and a list of all its child nodes
		XmlNodeList levelcontent = infoNode.ChildNodes;
		
		//now stept through that list and set all the node text
		foreach(XmlNode child in levelcontent){
			if(child.Name == "levelNumber"){
				child.InnerText = levelNumber.ToString();
			}
			if(child.Name == "name"){
				child.InnerText = systemName;
			}
			if(child.Name == "min"){
				child.InnerText = min;
			}
			if(child.Name == "bonusBronze"){
				child.InnerText = bronze;
			}
			if(child.Name == "bonusSilver"){
				child.InnerText = silver;
			}
			if(child.Name == "bonusGold"){
				child.InnerText = gold;
			}
			if(child.Name == "description"){
				child.InnerText = levelInfo.description;
			}
		}
		
		xmlDoc.Save (fileName);
		
		Debug.Log ("SAVED!!!!");

Guys I also faced the same problem with xml…

If i write something to my xml file it doesn’t get updated until i open some other application and than came back to unity. If someone know about this please let us know.

and @ lockbox
I did close the file after writing but still no difference. and i am writing data to an existing file.

VICTORY!!!

The problem wasn’t that the file wasn’t getting saved, it was that Unity’s TextAsset that represents the xml file wasn’t getting updated. So simply use

xmlDoc.Save (fileName);
AssetDatabase.Reload();

guys how if i wanted to write in xml the user’s action like user have clicked A, user has clicked B… pls help me am new to unity.

I’m experiencing the same problem, but AssetDatabase.Refresh doesn’t fix it (Reload() doesn’t seem to be available).
The content of the file only shows after i alt-tab out and back to unity.
For saving i use DataContractSerializer.WriteObject().
Even with AssetImportOptions.ForceUpdate it’s not working.
Anything else i can do?

Edit: AssetDatabase.ImportAsset() also doesn’t work.

I’m using this in an Editor-Window and it’s not very user friendly having to switch applications every time edits are saved from it.

In my experience, AssetDatabase.ImportAsset called on the path of the asset makes the change show up in the editor. That’s in 4.6, though.

You might try AssetDatabase.Refresh(ImportAssetOption.ForceUpdate), see if that does the trick.

I just found the problem:
i was refreshing/reimporting inside using(streamWriter){}
My bad…, sorry. But thanks for trying to help!