Loading a XML file (C#)

Hey all.
I’m trying to learn how to save and load XML files and show the different values from the XML file in the Inspector using a Custom Editor. I have learned how to save a XML file, but I have no idea how to load the xml file and assigning specific values to, i.e ScreenWidth as I would like to in this example.

settings.cs:

using UnityEngine;
using System.Collections;

public class settings : MonoBehaviour {

	public int ScreenWidth	=	1024;
	public int ScreenHeight	=	768;
	public bool fullscreen	=	true;
	
	public string settingsFilename = "settings.xml";
}

editor_settings.cs:

using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
using System.IO;
using System.Xml;

[CustomEditor(typeof(settings))]
[CanEditMultipleObjects]
public class editor_settings : Editor
{
	private settings _settings;
	
	void Awake()
	{
		_settings = (settings)target;	
	}
	
	public override void OnInspectorGUI()
	{
		_settings.ScreenWidth	=	EditorGUILayout.IntField ("Screen Width:", _settings.ScreenWidth);
		_settings.ScreenHeight	=	EditorGUILayout.IntField ("Screen Height:", _settings.ScreenHeight);
		_settings.fullscreen	=	EditorGUILayout.Toggle ("Fullscreen:", _settings.fullscreen);
		
		EditorGUILayout.Space ();
		_settings.settingsFilename = EditorGUILayout.TextField ("Filename:", _settings.settingsFilename);
		if(GUILayout.Button ("Generate XML file"))
		{
			WriteXMLfile ();
		}
		
		if(GUILayout.Button ("Load XML file"))
		{
			string filepath = Directory.GetCurrentDirectory () + @"/data/settings.xml";
			LoadXMLdata (filepath);
		}
	}
	
	public void WriteXMLfile()
	{
		string filepath = Directory.GetCurrentDirectory () + @"/data/" + _settings.settingsFilename;
		XmlDocument xmlDoc = new XmlDocument();
		
		Debug.Log ("OUTPUT: " + filepath);
		
		if(!File.Exists (filepath))
		{
			System.IO.Directory.CreateDirectory (Directory.GetCurrentDirectory() + @"/data");
			
			WriteXMLdata (filepath);
		}
		else
		{
			xmlDoc.RemoveAll ();
			
			WriteXMLdata (filepath);
			
			EditorUtility.DisplayDialog ("Generate XML file", "The following XML-file has been generated: " + filepath, "Ok");
		}
	}
	
	public void WriteXMLdata(string _path)
	{
		XmlDocument xmlDoc = new XmlDocument();
		
		XmlDeclaration declaration = xmlDoc.CreateXmlDeclaration ("1.0", null, null);
		xmlDoc.AppendChild (declaration);
		
		XmlElement root = xmlDoc.CreateElement ("ScreenSettings");
		xmlDoc.AppendChild (root);
		
		XmlNode node = xmlDoc.CreateNode (XmlNodeType.Element, "SCR", null);
		XmlAttribute atr1	=	xmlDoc.CreateAttribute ("ScreenWidth");
		XmlAttribute atr2	=	xmlDoc.CreateAttribute ("ScreenHeight");
		XmlAttribute atr3	=	xmlDoc.CreateAttribute ("Fullscreen");
		
		atr1.Value	=	_settings.ScreenWidth.ToString ();
		atr2.Value	=	_settings.ScreenHeight.ToString ();
		atr3.Value	=	_settings.fullscreen.ToString ();
		
		node.Attributes.Append (atr1);
		node.Attributes.Append (atr2);
		node.Attributes.Append (atr3);
		
		xmlDoc.DocumentElement.AppendChild (node);
		xmlDoc.Save (_path);
	}
	
	public void LoadXMLdata(string _path)
	{
		Debug.Log ("Load XML data and show it in the inspector");
		//_settings.ScreenWidth =
		//_settings.ScreenHeight =
		//_settings.fullscreen =
	}
}

The output is this, as I want it to be:

<?xml version="1.0"?>
<ScreenSettings>
  <SCR ScreenWidth="1024" ScreenHeight="768" Fullscreen="True" />
</ScreenSettings>

How would I go about to load this xml file to set _settings.ScreenWidth to the “ScreenWidth” value from the xml file?

Hope you’re following me… :stuck_out_tongue: I’m not… :wink:

Solved my own problem…

	public void LoadXMLdata(string _path)
	{
		XmlReader reader = XmlReader.Create (_path);
		
		while(reader.Read ())
		{
			if(reader.IsStartElement ("SCR"))
			{
				_settings.ScreenWidth = int.Parse (reader.GetAttribute ("ScreenWidth"));
				_settings.ScreenHeight = int.Parse (reader.GetAttribute ("ScreenHeight"));
				if(reader.GetAttribute ("Fullscreen") == "True")
				{
					_settings.fullscreen = true;
				}
				else
				{
					_settings.fullscreen = false;
				}
			}
		}
	}

Have you tried using XmlSerializer and just serializing/deserializing a new instance of settings and assigning the Inspector target to that? Would be a lot easier, flexible, extensible, and less error prone than reading the attributes manually.

Actually no… where can I read about it?

Read it here (documentations and examples): XmlSerializer Class (System.Xml.Serialization) | Microsoft Learn