Writing XML Log Files in Unity 3D using C#

You are here


The code for this project has seen extensive changes and has since been migrated to GitHub. Read more about it over here – and then go forth and fork it. It is released under a Creative Commons License, so you are free to build upon it.

Since I want to make some basic statistics for my game at Fantoche, I needed some basic logging function of the player’s position.

Of course, this could also be done using a simple CSV file, but the perfectionist in me insisted on an XML format. A preliminary test showed me, that I would be able to transform the XML to a CSV later on, so that my S. O. would be able to use it in his own programs.1

Having set up my development environment in MonoDevelop, I started to work on the problem on how to get my data into a well-formed XML representation and onto the hard disk.

The first approach was to use serialisation. I created a Location class, with all the necessary attributes – until I realised that this would only allow me to get one dataset into a file. I wouldn’t be able to add more data to the file.

Conclusion: Serialisation is only good when you have one clearly defined object you want to dump onto the drive as a well-formed XML file.

So I tried to work with the XMLWriter. This seemed more promising, but again I ran into problems: Since I wanted to write some meta data at the beginning of the file, the writer stream would somehow span over several methods … which was not really working – it wrote the meta data, and then closed the file again.

Finally, I found some help online at Microsoft on modifying large XML files:

The first technique I’ll demonstrate is targeted at situations where the goal is to be able to quickly append entries to an XML document. This approach involves creating two files. The first file is a well-formed XML file while the second is an XML fragment. The well-formed XML file includes the XML fragment using either an external entity declared in a DTD or using an xi:include element. This way the file containing the XML fragment can be updated efficiently by simply appending to it while processing is done using the including file.

This is now exactly what I did. The following code will write the wrapper XML file as well as prepare for the fragment:

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

public class SaveLocation : MonoBehaviour {

public GameObject trackedPlayer;
public GameObject calibration;
private XmlWriterSettings fragmentSettings;
private string logFilePath;
FileStream logfile;

// Use this for initialization
void Start ()
    XmlWriterSettings wrapperSettings = new XmlWriterSettings ();
    wrapperSettings.Indent = true;
    string basename = DateTime.Now.ToOADate ().ToString ();
    string wrappername = "tracker/wrapper-" + basename + ".xml";
    logFilePath = "tracker/log-" + basename + ".xml";

    trackedPlayer = GameObject.FindGameObjectWithTag ("Player");

    // Write the wrapper file
    GameObject ld = GameObject.Find ("LevelData");
    string version = ld.GetComponent<LevelData> ().levelVersion;
    string doctype = "<!DOCTYPE trackerdata \n [ \n <!ENTITY locations SYSTEM \"log-" + basename + ".xml\">\n ]>";

    using (XmlWriter writer = XmlWriter.Create (wrappername, wrapperSettings)) {
        writer.WriteRaw (doctype);  
        writer.WriteStartElement ("trackerdata");

        // meta
        writer.WriteStartElement ("meta");

        writer.WriteStartElement ("starttime");
        writer.WriteValue (DateTime.Now);
        writer.WriteEndElement ();

        writer.WriteElementString ("levelversion", version);

        writer.WriteEndElement ();
        // /meta

        writer.WriteStartElement ("tracking");
        writer.WriteEntityRef ("locations");            
        writer.Close ();

    // Prepare the log XML fragment
    logfile = new FileStream (logFilePath, FileMode.Append, FileAccess.Write, FileShare.Read);

    fragmentSettings = new XmlWriterSettings ();
    fragmentSettings.ConformanceLevel = ConformanceLevel.Fragment;
    fragmentSettings.Indent = true;
    fragmentSettings.OmitXmlDeclaration = false;

    InvokeRepeating("CollectData", 0, 5);

The previous code will produce an XML file along the lines of this:

]> 2010-05-11T18:37:04.9179390+02:00 0 &locations;

The function CollectData() being called looks like this:

void CollectData () {

    using (XmlWriter writer = XmlWriter.Create (logfile, fragmentSettings)) {
        writer.WriteStartElement ("location");

        writer.WriteStartAttribute ("runningTime");
        writer.WriteValue (Time.timeSinceLevelLoad);
        writer.WriteEndAttribute ();

        writer.WriteStartElement ("x");
        writer.WriteValue (trackedPlayer.transform.position.x);
        writer.WriteEndElement ();

        writer.WriteStartElement ("y");
        writer.WriteValue (trackedPlayer.transform.position.y);
        writer.WriteEndElement ();

        writer.WriteStartElement ("z");
        writer.WriteValue (trackedPlayer.transform.position.z);
        writer.WriteEndElement ();

        writer.WriteEndElement ();
        writer.Flush ();


    logfile.Flush ();

This function produces the following output:

-5.080078 -23.35105 0.6675512 -5.080078 -23.35105 0.6675512 -5.080078 -23.35105 0.6675512

The script does more or less what it is supposed to do. Still on the list of desirable features are:

  • The ability to call CollectData() at any time, and passing a string why it was called (used to track special occasions, like the death of the avatar or reaching a trigger). This could be done by method overloading.

In hindsight, using XMLDocument might have been wiser, but since I didn’t know about that possibility when writing the script, I solved it with the XMLWriter.

  1. So why again am I doing it with XML? Good question. Because I can? Does that make me a nerd? ↩︎