Writing and reading to XML

Ok so I am trying to learn and warp my head around XML so I can store data more efficiently than playerprefs, because populating the registry with hundreds of account names, passwords, and “what not” doesn’t seem very ethical or organized.

I have been searching since yesterday about tutorials and everything and finally found a good one. I have accomplished loading my XML, as well as clearing everything inside the root element. However, when I try to write to the XML it seems to not work.

So the EraseData function works 100% and just leaves the root element behind looking liek this in the xml:

<?xml version="1.0"?>
<accounts>
</accounts>

But when I press my write Ring’s account button and run the “WriteAccountToXml” function, the XML document stays completely blank like above. Why is it not writing in the new information? (Note: the debug.log at the end does show up, so idk whats wrong.)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.IO;

public class xmlLoader : MonoBehaviour {
public string XMLFolder = "Server_data";
public string XMLFileName = "Accounts.xml";
private string newXMLPath = "";
	
	void Start () {
		//create xml path
		newXMLPath = Application.dataPath+"/"+XMLFolder+"/"+XMLFileName;
	}
	
	void OnGUI () {
		GUI.Box(new Rect(10,10,500,400), "");
		GUI.Label(new Rect( 20, 20,475, 300), "Path: "+newXMLPath);
		if(GUI.Button(new Rect(70,60,400, 50), "Erase All Account Data")) {
			EraseData();
		}
		if(GUI.Button(new Rect(70,120,400, 50), "Write RingOfStorms' Account")) {
			WriteAccountToXml();
		}
	}	
	
	private void EraseData () {
		XmlDocument xmlDoc = new XmlDocument();
		
		if(File.Exists (newXMLPath)) {
			xmlDoc.Load(newXMLPath); //Load the XML file
			
			XmlElement elmRoot = xmlDoc.DocumentElement; //Find the XML's element (aka the root)
			
				elmRoot.RemoveAll(); //Remove all accounts DANGER DANGER DANGER (but lets see how this works =D)
				
				xmlDoc.Save(newXMLPath); //save xml file.
		}
	}
	
	private void WriteAccountToXml () {
		XmlDocument xmlDoc = new XmlDocument();
		
		if(File.Exists (newXMLPath)) {
			xmlDoc.Load(newXMLPath); //load xml
			
			XmlElement newAccount = xmlDoc.CreateElement("account"); //create account element
			
			//Child Elements for new account
			XmlElement newTitle = xmlDoc.CreateElement("title"); // title element
				newTitle.InnerText = "Admin";
				
			XmlElement newName = xmlDoc.CreateElement("name"); // title element
				newName.InnerText = "RingOfStorms";
				
			XmlElement newPassword = xmlDoc.CreateElement("password"); // title element
				newPassword.InnerText = "Password1";
				
			XmlElement newLastLocation = xmlDoc.CreateElement("lastLocation"); // title element
				newLastLocation.InnerText = "0,0,0";
				
			XmlElement newLastScene = xmlDoc.CreateElement("lastScene"); // title element
				newLastScene.InnerText = "0";
				
			XmlElement newOther = xmlDoc.CreateElement("other"); // title element
				newOther.InnerText = "Game Creator. Oh YEA!!!!!";
				
				
			//Assign the new child elements as child elements for the element new account
				newAccount.AppendChild(newTitle); //assign the child element to the new account
				newAccount.AppendChild(newName);
				newAccount.AppendChild(newPassword);
				newAccount.AppendChild(newLastLocation);
				newAccount.AppendChild(newLastScene);
				newAccount.AppendChild(newOther);
				
			//YOU MUST SAVE IT DUMMIES
			xmlDoc.Save(newXMLPath);
			
			Debug.Log("Writting Ring's account to: "+xmlDoc);
		}
	}
}

It seems CreateElement does not attach the element to the document (as per the documentation and example on MSDN XmlDocument.CreateElement Method (System.Xml) | Microsoft Learn ). So your element newAccount is not attached to your document.

You need to also add the element to the document using a method such as AppendChild().

Sweet thanks Jonny, I got the element root and added the account after I defined it to make sure it was in there first and it works like a charm.

Just put your data in a class and use standard .NET serialization to read and write it. Look up XmlSerializer, StreamWriter, and FileStream.

Here’s an example of using them together:

http://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm