如何在同一位置读取和写入xml文件? [英] How to read and write a xml file into the same location?

查看:164
本文介绍了如何在同一位置读取和写入xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经准备了具有某些内容的xml文件,并希望在iOS设备上播放时将其加载,但是我还想更改已加载的数据并再次将其序列化到同一文件中. 在Unity编辑器(Windows)中,它可以完美运行,但是当我在iOS设备上对其进行测试时,似乎可以使用WWW类从StreamingAssets中读取内容,但无法对其进行写入. 我还发现我可以读写Application.persistentDataPath创建的路径.但是似乎该位置位于设备中的某个位置,我无法将xml放入该位置,并且用户可以访问该文件夹,所以这不是一个好的解决方案,不是吗?

I have prepared xml files with some content and want to load it while playing on iOS device but also I want to change loaded data and serialize it in the same file again. In Unity Editor (Windows) it works perfectly, but when I test it on iOS device it seems that I can read from StreamingAssets using WWW class, but I can't write into it. Also I have found that I can read and write into path created by Application.persistentDataPath. But it seems that location somewhere in device and I can't put my xml into that location and users have access to that folder so that isn't good solution, isn't it?

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

Here code that I use to load and save the data.

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;
}
}

推荐答案

我想将我的xml文件放入此文件夹中,然后读取它.就像游戏的默认信息一样"

"I want to put my xml file in this folder, and then read it. It's like default info for game"

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

easy, just put it in your assets. go like this...

 public TextAsset myXMLFile;

在检查器中

将文件拖到那里.完成了.

in Inspector drag the file there. You're done.

但是我也想更改该文件并保存"

"but then I also want to change that file and save"

足够公平.你要做的就是

Fair enough. What you have to do is

(1)创建路径p = Application.persistentDataPath +"values.txt"

(1) make a path p = Application.persistentDataPath + "values.txt"

(2)程序启动.

(3)检查"p"是否存在.如果是,请阅读并转到(6)

(3) check if "p" exists. if yes, read it and go to (6)

(4)如果不是,请阅读文本资产并将其保存到"p"

(4) IF NOT, read the textasset and save that to "p"

(5)转到第(3)点

(6)完成了.

这是唯一的方法.实际上,这确实是Unity中的正常过程,您可以在每个Unity应用程序中进行此操作.没有别的办法!

It's the only way to do it. This is indeed the normal procedure in Unity, you do it in every Unity app. There's no other way!

这篇关于如何在同一位置读取和写入xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆