使用Unity(使用C#)将XML文件写入iOS
我试图在iOS上编写XML,但这似乎是不可能的。尝试了几种不同的方法,但都没有效果 我读到iOS只对其/Documents文件夹具有写访问权限,因此我尝试将Application.dataPath设置为Documents文件夹。但是我在Xcode中遇到了这个错误:使用Unity(使用C#)将XML文件写入iOS,c#,ios,xml,ipad,unity3d,C#,Ios,Xml,Ipad,Unity3d,我试图在iOS上编写XML,但这似乎是不可能的。尝试了几种不同的方法,但都没有效果 我读到iOS只对其/Documents文件夹具有写访问权限,因此我尝试将Application.dataPath设置为Documents文件夹。但是我在Xcode中遇到了这个错误: DirectoryNotFoundException: Could not find a part of the path "/Users/mariusmathisen/Library/Application Support/iPho
DirectoryNotFoundException: Could not find a part of the path "/Users/mariusmathisen/Library/Application Support/iPhone Simulator/7.1/Applications/6AE4AF70-EFDF-46F0-9947-5C0936F3AD27/DaXml.app/Data/Documents/gamexmldata.txt".
这是我要写入XML的代码:
lic void WriteToXml()
{
Debug.Log ("Du nådde metoden");
//string filepath = Application.dataPath + @"/Data/gamexmldata.xml";
XmlDocument xmlDoc = new XmlDocument();
GameControl.control.outputXml = "Du nådde WriteXML";
if(File.Exists(Application.dataPath + "/Documents/gamexmldata.txt"))
{
Debug.Log("Filen Fantes");
GameControl.control.outputXml = "DU ER I Writetoxml og filen fantes";
xmlDoc.Load(Application.dataPath + "/Documents/gamexmldata.txt");
//xmlDoc.Load(filepath);
XmlElement elmRoot = xmlDoc.DocumentElement;
// elmRoot.RemoveAll(); // remove all inside the transforms node.
XmlElement elmNew = xmlDoc.CreateElement("rotation"); // create the rotation node.
XmlElement rotation_X = xmlDoc.CreateElement("x"); // create the x node.
rotation_X.InnerText = x; // apply to the node text the values of the variable.
XmlElement rotation_Y = xmlDoc.CreateElement("y"); // create the y node.
rotation_Y.InnerText = y; // apply to the node text the values of the variable.
XmlElement rotation_Z = xmlDoc.CreateElement("z"); // create the z node.
rotation_Z.InnerText = z; // apply to the node text the values of the variable.
XmlElement navnElement = xmlDoc.CreateElement("name");
navnElement.InnerText = navn;
GameControl.control.inputXml = navnElement.InnerText;
elmNew.AppendChild(rotation_X); // make the rotation node the parent.
elmNew.AppendChild(rotation_Y); // make the rotation node the parent.
elmNew.AppendChild(rotation_Z); // make the rotation node the parent.
elmNew.AppendChild(navnElement);
elmRoot.AppendChild(elmNew); // make the transform node the parent.
xmlDoc.Save(Application.dataPath + "/Documents/gamexmldata.txt"); // save file.
Debug.Log("Filen er lagret");
}else{
xmlDoc.Save(Application.dataPath + "/Documents/gamexmldata.txt");
}
}
对于为什么我不能将XML写入iOS及其文件夹,有什么建议吗?我做错了什么?您应该能够使用
应用程序将数据保存到磁盘。persistentDataPath
。它会转到公用文件夹,在更新应用程序时不会被删除 我认为问题在于你的道路并不完全正确
根据苹果公司的规定,您需要将文件写入:
/Documents/gamexmldata.txt
相反,您将其写为:
/[AppName].app/Documents/gamexmldata.txt
要解决此问题,可以使用以下方法:
fileSaveLocation=Application.dataPath.Replace(“/[AppName].app/Data”,“/Documents/gamexmldata.txt”)
为了确保-[AppName]这是您在构建应用程序时选择的开发应用程序Id名称(不是应用程序的“名称”,而是Apple的Id名称)
有关Apple所需位置的文档可在此处找到:
让我知道它是否有用。它现在可以工作,并且在iOS模拟器上它可以读取和写入XML文件,但是当我在设备上测试时,我得到以下错误:XmlException:Document元素没有出现。file:///var/mobile/Applications/47C26549-9CB0-46C2-9906-66399A5E515C/Documents/gamexmldata.txt 第1行,位置1。