Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Help with XML for Save/Load

Discussion in 'Scripting' started by VengarlofForossa, Aug 3, 2023.

  1. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    Hello Everyone!

    I have a problem with XML for saving/loading data. I have created the following XML with Unity. This XML is created when the game is first opened.

    Code (CSharp):
    1.        
    2. if(!File.Exists(routeName))
    3.         {
    4.             FileStream xmlStream = File.Create(routeName);
    5.  
    6.                 XmlWriter xmlWriter = XmlWriter.Create(xmlStream);
    7.  
    8.                 xmlWriter.WriteStartDocument();
    9.                     xmlWriter.WriteStartElement("profile");
    10.                         xmlWriter.WriteElementString("profileCreated", "0");
    11.                         xmlWriter.WriteElementString("nameprofile", "0");
    12.                         xmlWriter.WriteElementString("timePlayed", "0");
    13.                     xmlWriter.WriteEndElement();
    14.                 xmlWriter.Close();
    15.             xmlStream.Close();
    16.         }
    17.  
    This code works perfectly and creates the following XML.

    Code (CSharp):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <profile>
    4.     <profileCreated>0</profileCreated>
    5.     <nameprofile>0</nameprofile>
    6.     <timePlayed>0</timePlayed>
    7. </profile>
    8.  
    The problem is that I don't know how to obtain this information. I have tried to create this script but it gives me an error: InvalidOperationException: <profile xmlns=''> was not expected.

    How can I do this? I don't have much idea about this part and as much as I try to look for information I find it quite confusing.

    Code (CSharp):
    1.        
    2.         _dataPath = Path.Combine(Application.persistentDataPath + "/playerData/");
    3.         string _xmlInfo = _dataPath + "profileGame.xml";
    4.         Debug.Log(_xmlInfo);
    5.         if (File.Exists(_xmlInfo))
    6.         {
    7.             var xmlSerializer = new XmlSerializer(typeof(string));
    8.  
    9.             using (FileStream stream = File.OpenRead(_xmlInfo))
    10.             {
    11.                 var profileInfo = (string)xmlSerializer.Deserialize(stream);
    12.                
    13.                 //Fill in the variables here...
    14.                 // profileCreated;
    15.                 // nameProfile;
    16.                 // timePlayed;
    17.             }
    18.         }
    19.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    A pre-question before we try to dig into your actual question: Are you committed to using XML for your save/load?

    I ask because JSON is a far more widely used standard, especially in games. As a result there are TONS of JSON-based tools that are optimized to run in Unity and C#. XML is a far more fiddly, less popular, and less tool-rich format.

    For comparison, with the JSON tools available like Newtonsoft's Json.NET, your save and load functions can be literally 2 lines of code, each.
     
    VengarlofForossa likes this.
  3. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    First you're using an
    XmlWriter
    and then a
    XmlSerializer
    . Those are different concepts, the first processes an Xml at a lower level, creating elements and values manually, the later automatically turns objects into Xml.

    If you're using
    XmlWriter
    , you'd use XmlReader to read the Xml back.

    If you're using
    XmlSerializer
    , you'd create a class that you pass instead of
    typeof(string)
    and then use the
    Serialize
    and
    Deserialize
    methods to create and load Xml files.

    But I'd recommend against using Xml, it's generally harder to work with than e.g. Json or Yaml. The simplest approach for save/load in Unity is IMO to use a ScriptableObject to store everything you want to save and then JsonUtility to write/read that to/from a file.
     
    VengarlofForossa and ZO5KmUG6R like this.
  4. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    This project is not for uploading to the cloud, so I decided to try XML. For now, I'd like to try to learn the XML part, even if it's just the basics.
    Later on, I'll try JSON because I'd like to learn both.
     
  5. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    OK, I understand. Thank you very much for your answer!
    I also have to say that English is not my mother tongue, and this often makes it difficult for me to learn.

    I think I'm going to try to start from scratch but with JSON.