如何读取和写入一个XML文件到相同的位置?

我准备了一些内容的XML文件,并希望加载它在iOS设备上玩,但我也想改变加载的数据,并在同一个文件再次序列化。 在Unity编辑器(Windows)中,它完美的工作,但是当我在iOS设备上testing它时,似乎我可以使用WWW类从StreamingAssets读取,但是我无法写入它。 另外我发现我可以读写由Application.persistentDataPath创build的path。 但似乎在设备的位置,我不能把我的XML到该位置,用户有权访问该文件夹,所以这不是很好的解决scheme,不是吗?

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

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文件放在这个文件夹中,然后读取它,就像游戏的默认信息”

容易,只需把它放在你的资产。 像这样去…

  public TextAsset myXMLFile; 

在Inspector中拖动文件。 你完成了。


“但是我也想改变那个文件并保存”

很公平。 你必须做的是

(1)build立一个pathp = Application.persistentDataPath +“values.txt”

(2)计划启动。

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

(4)如果不是,读取textasset并保存为“p”

(5)去点(3)

(6)你完成了。

这是做到这一点的唯一方法。 这确实是Unity中的正常程序,你可以在每个Unity应用程序中执行。 没有别的办法

Interesting Posts