XmlHelper

From Headbone Creative
Revision as of 04:48, 21 April 2013 by Tyson Joehler (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This class allows the programmer to easily read and write data structures to and from XML files with a single line of code.

Contents

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");

Public Functions

This is a generic class, so in the functions below, T refers to the type of object being serialized or unserialized.

static void Save(T obj, string fileName)

This saves the object to the file with the name specified.

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.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox