如何读取和写入xml文件到同一位置?

时间:2016-05-13 18:19:22

标签: ios xml serialization unity3d deserialization

我已准备好包含某些内容的xml文件,并希望在iOS设备上播放时加载它,但我还想更改加载的数据并再次在同一文件中将其序列化。 在Unity Editor(Windows)中它可以很好地工作,但是当我在iOS设备上测试它时,似乎我可以使用WWW类从StreamingAssets读取,但我无法写入它。 另外我发现我可以读写Application.persistentDataPath创建的路径。但它似乎位于设备的某个位置,我无法将我的xml放入该位置,用户可以访问该文件夹,因此这不是一个好的解决方案,不是吗?

这是我用来加载和保存数据的代码。

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

public class testxml : MonoBehaviour {

public Text result;
public InputField firstPart, secondPart;
public Toggle toggle;

private List<int> listToSave;

// Use this for initialization
void Start () {
    listToSave = new List<int>();
}

public void Save()
{
    Serialize();
}

public void Load()
{
    StartCoroutine(Deserialize());
}

private void Serialize()
{
    string path = GetPath();
    try
    {
        Debug.Log("trying to save");

        var serializer = new XmlSerializer(typeof(List<int>));
        using (var fs = new FileStream(path, FileMode.OpenOrCreate))
        {
            serializer.Serialize(fs, listToSave);
        }
    }
    catch (XmlException e)
    {
        result.text = "error";
        Debug.LogError(path + "  with " + (toggle.isOn ? "persistent data path" : "data path"));
        Debug.LogError("xml exc while des file : " + e.Message);
    }
    catch (System.Exception e)
    {
        result.text = "error";
        Debug.LogError("exc while des file : " + e.Message);
        Debug.LogError(path + "  with " + (toggle.isOn ? "persistent data path" : "data path"));
        System.Exception exc = e.InnerException;
        int i = 0;
        while (exc != null)
        {
            Debug.Log("inner " + i + ": " + exc.Message);
            i++;
            exc = exc.InnerException;
        }
    }
}

private IEnumerator Deserialize()
{
    Debug.Log("trying to load");
    string path = GetPath();
    var www = new WWW(path);
    yield return www;
    if (www.isDone && string.IsNullOrEmpty(www.error))
    {
        try
        {
            var serializer = new XmlSerializer(typeof(List<int>));
            MemoryStream ms = new MemoryStream(www.bytes);
            listToSave = serializer.Deserialize(ms) as List<int>;
            ms.Close();
            result.text += "Done\n";
            foreach (var i in listToSave)
                result.text += i + "\n";
        }
        catch (XmlException e)
        {
            result.text = "error";
            Debug.LogError(path + "  with " + (toggle.isOn?"persistent data path":"data path"));
            Debug.LogError("xml exc while des file : " + e.Message);
        }
        catch (System.Exception e)
        {
            result.text = "error";
            Debug.LogError("exc while des file : " + e.Message);
            Debug.LogError(path + "  with " + (toggle.isOn ? "persistent data path" : "data path"));
            System.Exception exc = e.InnerException;
            int i = 0;
            while(exc!=null)
            {
                Debug.Log("inner "+i+": " + exc.Message);
                i++;
                exc = exc.InnerException;
            }
        }

        yield break;
    }
    else
    {
        Debug.LogError("www exc while des file " + www.error);
        Debug.LogError(path + "  with " + (toggle.isOn ? "persistent data path" : "data path"));
        yield break;
    }
}

private string GetPath()
{
    string path = firstPart.text;
    if (toggle.isOn)
    {
        path += Application.persistentDataPath;
    }
    else
        path += Application.dataPath;
    path += secondPart.text;
    return path;
}
}

1 个答案:

答案 0 :(得分:2)

  

&#34;我想把我的xml文件放在这个文件夹中,然后阅读它。它就像游戏的默认信息&#34;

很简单,只需将其放入您的资产中即可。像这样......

 public TextAsset myXMLFile;
在Inspector中

将文件拖到那里。你已经完成了。

  

&#34;但我还想更改该文件并保存&#34;

足够公平。你要做的是

(1)制作路径p = Application.persistentDataPath +&#34; values.txt&#34;

(2)程序启动。

(3)检查&#34; p&#34;存在。如果是,请阅读并转到(6)

(4)如果不是,请阅读textasset并将其保存到&#34; p&#34;

(5)转到第(3)点

(6)你已经完成了。

这是唯一的方法。这确实是Unity中的正常程序,您可以在每个Unity应用程序中执行此操作。别无他法!