Difference between revisions of "XmlHelper"
From Headbone Creative
(Initial page, includes API information and categories) |
Latest revision as of 04:48, 21 April 2013
This class allows the programmer to easily read and write data structures to and from XML files with a single line of code.
Contents |
[edit] Usage
To use this class, first create a simple data class and give it the attribute [System.Serializable]
[System.Serializable] public class SaveData { public string playerName = ""; public int highScore = 0; };
This is the class that will be serialized and unserialized to the file.
To save the data to a file, use the function Save:
SaveData saveObj = new SaveData (); XmlHelper<SaveData>.Save (saveObj, "path/to/save/file.xml");
To load the data again, use the Load function:
SaveData restoredObj = XmlHelper<SaveData>.Load ("path/to/save/file.xml");
[edit] Public Functions
This is a generic class, so in the functions below, T refers to the type of object being serialized or unserialized.
[edit] static void Save(T obj, string fileName)
This saves the object to the file with the name specified.
[edit] static T Load(string xml)
This attempts to load an object from a file with the specified name. It will return null if the file cannot be read or does not exist.