Data Serialization

Hi everyone!

I’m trying to make an xml file to save some data on my game, on Unity Editor i didnt had any trouble, and i was able to generate the file, load it and re save it, but it doesnt seem to be working on Android and i dont know why.

I’m Usint System.xml.serialization.

This is my Saving Method:

    public void SaveXml(string filename)
    {
        //string path = Path.Combine(Application.dataPath, filename);

        string path = Application.dataPath + filename;

        XmlSerializer serializer = new XmlSerializer(typeof(DataSerializer));

        // TODO: Check Diferent Filemodes (Append, Check if file exists)
        using (FileStream stream = new FileStream(path, FileMode.Create))
        {
            serializer.Serialize(stream, this);
        }
    }

And the Loading:

public static DataSerializer LoadXml(string path)
    {
        var serializer = new XmlSerializer(typeof(DataSerializer));

        using (FileStream stream = new FileStream(path, FileMode.Open))
        {
            return serializer.Deserialize(stream) as DataSerializer;
        }
    }

I’ve modified the loading and made a new method to get a string and deserialize it from a text, but it doesnt work on android also.
and i dont know how make the “Save” of the xml pointing to the resources folder

    public static DataSerializer LoadXmlFromFile(string filename)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(DataSerializer));

        TextAsset text = Resources.Load(filename) as TextAsset;

        if (text != null)
        {
            using (Stream s = new MemoryStream(text.bytes))
            {
                return serializer.Deserialize(s) as DataSerializer;
            }
        }
        else
        {
            Debug.LogWarning(string.Format("Unable to load resource {0}", filename));

        }

        return null;

    }

does anyone have some info, or link to some tutorial on how to achieve this???

I need to read/write a file with data about the game like, purchased cars, personalization made to each one etc.

Any hints on this would be really appreciated!

Thanks in advance!

I built an asset called XMLEncrypto its free. It should work on Android.

If you don’t want to use it you can use Application.dataPath or Application.persistentDataPath to save and load from.

maybe XmlSerializer isn’t supported at runtime?

http://docs.unity3d.com/412/Documentation/ScriptReference/MonoCompatibility.html

As already posted in my thread:

  • Android supports XmlSerializer
  • You cannot modify data that can be loaded through Resources.Load at runtime (they are packaged at built time)
  • To get a crossplattform path to store your data use Application.persistentDataPath.

You say loading doesn’t work on android. What exactly happens? Any errors? Did you make sure to pass the correct name to Resources.Load, is the file in your resources folder and has a name that is supported by unity? (Keep in mind .xml isn’t a file extension recognized by unity, so putting a data.xml file into resources won’t get packaged into the application).