简体   繁体   中英

XML file not getting written using Unity and C#

I am trying to save a list into XML and later load the XML back into that list. Most of it appears to work well with the exception being the actual Save method.

Here is my code. The problem is that SaveInformation.xml is not getting created.

[XmlRoot("SaveInformation")]
public class SaveInformation {

    [XmlArray("stat")]
    [XmlArrayItem("PlayerData")]
    public PlayerData[] stat;

    public void Save(string path){
        var serializer = new XmlSerializer(typeof(SaveInformation));
        using(var stream = new FileStream(path,FileMode.Create)){
            serializer.Serialize (stream, this);
            vstream.Close();
        }
    }

    public static SaveInformation Load(string path){
        var serializer = new XmlSerializer(typeof(SaveInformation));
        using(var stream = new FileStream(path,FileMode.Open)){
            return serializer.Deserialize (stream) as SaveInformation;
            stream.Close();
        }
    }
}

Here are the Load and Save methods:

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

public class StoreData : MonoBehaviour {

    public static void Load(){
        var saveInformation = SaveInformation.Load(
            Path.Combine(Application.dataPath, "SaveInformation.xml"));

        Debug.Log("Loaded");
    }

    public static void Save(){
        SaveInformation obj = new SaveInformation();

        obj.Save(Path.Combine(Application.persistentDataPath,
            "SaveInformation.xml"));

        Debug.Log("Saved");
    }
}

Here is the Class that should be passing XML stat and PlayerData

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("PlayerDatabaseCollection")]
public class PlayerDatabase : MonoBehaviour {
    [XmlArray("stat")]
    [XmlArrayItem("PlayerData")]
    public List<PlayerData> stat = new List<PlayerData>();
}

And here is the Data itself

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;

[System.Serializable]
public class PlayerData {
    [XmlAttribute("name")]
    public string Name;
    [XmlAttribute("current")]
    public int Current;
    [XmlAttribute("max")]
    public int Max;
}

You shouldn't create a new instance of your class if you call save. Make sure you only have one instance that gets Loaded and Saved...

public class StoreData : MonoBehaviour {

    // this will hold your data (but in the snippets you provided I 
    // can't see how you are accessing this, maybe this should be public)
    private static SaveInformation saveInformation = null; // keep one instance

    private static string savepath = Application.dataPath; // or Application.persistentDataPath ?
    private static string savePathFile = Path.Combine(savepath, "SaveInformation.xml");

    public SaveInformation Instance {
       get {
           return (saveInformation ?? new SaveInformation());
       }
    }

    public static void Load(){
        saveInformation = SaveInformation.Load(savePathFile);
        Debug.Log("Loaded");
    }

    public static void Save(){
        if (saveInformation != null) {
           saveInformation.Save(savePathFile);
           Debug.Log("Saved");
        } else {
            Debug.Log("No saveinformation yet");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM