Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by mmangual_83 · Dec 04, 2013 at 02:23 PM · c#serializationxmlfile-io

IsolatedStorageException: Could not find a part of the path

I want to be able to save a file into my executable folder. But it doesn't save the file and throws the exception "IsolatedStorageException: Could not find a part of the path..."

When I run this application using the Unity Editor it saves it onto the correct save file which is located on the Asset folder but when I run it in Build it doesn't save it.

Here is the code I have:

 using UnityEngine;
 using System.Collections;
 using System.Xml;
 using System.Xml.Serialization;
 using System.IO;
 using System.Text;  
 using System.Collections.Generic;
 
 public class TrainingData : MonoBehaviour 
 {
     string _FileLocation,_FileName;
     string _data;
 
     UserData iData = new UserData();
     
     // When the EGO is instansiated the Start will trigger 
     // so we setup our initial values for our local members 
     void Start () {
         
         // Where we want to save and load to and from 
         _FileLocation=Application.dataPath; 
         _FileName="SaveData.xml";
     } 
     
     void Update () {}     
     
     public void Save(int userID, int x, int y, int z)
     {
         if (!Load())
             return;
         _data = string.Empty;
 
         bool isUserFound = false;
         foreach (var user in iData._users)
         {
             // Time to creat our XML!             
             if (userID == user.userID)
             {
                 user.userID = userID;
                 user.x = x;
                 user.y = y;
                 user.z = z;
 
                 isUserFound = true;
             }
             //_data += SerializeObject(user);
             
         }
         if (!isUserFound)
         {
             UserData.mUsers newUser = new UserData.mUsers();
             newUser.userID = userID;
             newUser.x = x;
             newUser.y = y;
             newUser.z = z;
             iData._users.Add(newUser);
         }
 
         _data = SerializeObject(iData);
         // This is the final resulting XML from the serialization process 
         CreateXML();
         Debug.Log(_data);          
 
     }
     private bool Load()
     {
         // Load our UserData into myData 
         LoadXML();
         if (_data.ToString() == "")
         {
             return false;
         }
         // notice how I use a reference to type (UserData) here, you need this 
         // so that the returned object is converted into the correct type
         var data = FixData();
         iData._users.Clear();
         var counter = 0;
         foreach (var dataToParse in data)
         {
             counter++;
             if (counter == 1) continue;
 
             if (string.IsNullOrEmpty(dataToParse.Trim())) continue;
             var uData = (UserData)DeserializeObject(dataToParse);
             iData._users.Add(uData._users[0]);
             //var ud = iData._users[iData._users.Count - 1];
             // just a way to show that we loaded in ok
         }        
         return true;
     }
     string[] FixData()
     {
         var dataSplit = _data.Split(new string[] { "<mUsers>", "</mUsers>" }, System.StringSplitOptions.RemoveEmptyEntries);
         dataSplit[0] = dataSplit[0].Replace("<_users>", string.Empty);
         for (var indx = 1; indx < dataSplit.Length; indx++ )
         {
             if (string.IsNullOrEmpty(dataSplit[indx].Trim()) || !dataSplit[indx].Contains("x"))
             {
                 dataSplit[indx] = string.Empty;
                 continue;
             }
 
             dataSplit[indx] = dataSplit[0] + "<_users><mUsers>" + dataSplit[indx] + "</mUsers></_users></UserData>";
             Debug.Log(dataSplit[indx]);
         }
 
         return dataSplit;
     }
     
      
     string UTF8ByteArrayToString(byte[] characters) 
     {      
         UTF8Encoding encoding = new UTF8Encoding(); 
         string constructedString = encoding.GetString(characters); 
         return (constructedString); 
     } 
     
     byte[] StringToUTF8ByteArray(string pXmlString) 
     { 
         UTF8Encoding encoding = new UTF8Encoding(); 
         byte[] byteArray = encoding.GetBytes(pXmlString); 
         return byteArray; 
     } 
 
     //// Here we serialize our UserData object of myData 
     string SerializeObject(UserData userData)
     {
         string XmlizedString = string.Empty;
         
         XmlSerializer xs = new XmlSerializer(typeof(UserData));
         MemoryStream memoryStream = new MemoryStream();
         XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
         xs.Serialize(xmlTextWriter, userData);
         memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
 
         XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
         //foreach (var user in l_userData)
         //{
             
         //}
         
         return XmlizedString;
     }
 
     // Here we deserialize it back into its original form 
     object DeserializeObject(string pXmlizedString) 
     { 
         XmlSerializer xs = new XmlSerializer(typeof(UserData)); 
         MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString)); 
         //XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); 
         return xs.Deserialize(memoryStream); 
     } 
     
     // Finally our save and load methods for the file itself 
     void CreateXML() 
     { 
         StreamWriter writer;
         
         FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName); 
         if(!t.Exists) 
         { 
             writer = t.CreateText();
         } 
         else 
         { 
             t.Delete(); 
             writer = t.CreateText(); 
         } 
         writer.Write(_data); 
         writer.Close(); 
         Debug.Log("File written."); 
     } 
     
     void LoadXML() 
     {
         StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName); 
         string _info = r.ReadToEnd(); 
         r.Close(); 
         _data=_info; 
         Debug.Log("File Read"); 
     } 
 } 
 
 // UserData is our custom class that holds our defined objects we want to store in XML format 
 public class UserData 
 {
     public List<mUsers> _users = new List<mUsers>();
 
     public class mUsers
     {
         // We have to define a default instance of the structure    
         public int userID;
         public int x;
         public int y;
         public int z;
     }
     
     // Default constructor doesn't really do anything at the moment 
     public UserData() { } 
 }

Can anyone see what I am missing. I do believe that the problem is that it is not creating the xml file at all, but I am not sure. Thank you in advance.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hitarthdoc1994 · Jan 27, 2016 at 10:27 AM 0
Share

Got the Same Problem.

Someone Please Help.

Thanks.

avatar image cstlmode · Jun 25, 2016 at 05:44 PM 0
Share

did you find a solution , i'm struggling with the same problem for days

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Making my XML file multiline 0 Answers

Best practice to store and load a specific set of objects? 3 Answers

How do I keep references to unique NPCs between scenes? 1 Answer

Serializing an Action as XML? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges