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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

XML serialization error while trying to read data

Discussion in 'Scripting' started by Erudaki, Sep 10, 2018.

  1. Erudaki

    Erudaki

    Joined:
    Apr 26, 2013
    Posts:
    3
    Hello. Fairly new to unity and was trying to read some data in from a file. I found this https://forum.unity.com/threads/saving-and-loading-data-xmlserializer.85925/ which i was using to help set up a base data read/write structure. It is currently not throwing any errors, but after I call the load function, I try to query the resulting information to find that it is not being set.

    Can anyone tell me what I'm doing wrong?


    WeaponContainer.Cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Xml;
    4. using System.Xml.Serialization;
    5. using System.IO;
    6. using UnityEngine;
    7.  
    8. [XmlRoot("WeaponCollection")]
    9. public class WeaponContainer {
    10.  
    11.     [XmlArray("Weapons"), XmlArrayItem("Weapon")]
    12.     public Weapon[] Weapons;
    13.  
    14.     public void Save(string path)
    15.     {
    16.         var serializer = new XmlSerializer(typeof(WeaponContainer));
    17.         using (var stream = new FileStream(path, FileMode.Create))
    18.         {
    19.             serializer.Serialize(stream, this);
    20.         }
    21.     }
    22.  
    23.     public static WeaponContainer Load(string path)
    24.     {
    25.         var serializer = new XmlSerializer(typeof(WeaponContainer));
    26.         using (var stream = new FileStream(path, FileMode.Open))
    27.         {
    28.             return serializer.Deserialize(stream) as WeaponContainer;
    29.         }
    30.     }
    31.  
    32. }
    33.  
    Weapon.Cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Xml;
    4. using System.Xml.Serialization;
    5. using System.IO;
    6. using UnityEngine;
    7.  
    8. public class Weapon {
    9.     [XmlAttribute("name")]
    10.     public string Name;
    11.     public float Damage;
    12.     public float AttackSpeed;
    13.     public float Range;
    14.     public bool bIsRanged;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.        
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update () {
    23.        
    24.     }
    25. }
    Load Function in another class
    Code (CSharp):
    1. public void fetchWeapons()
    2.     {
    3.         var weaponCollection = WeaponContainer.Load(Path.Combine(Application.dataPath, "XML/Weapons.xml"));
    4.         Debug.Log("Weapons");
    5.         Debug.Log(weaponCollection.Weapons[0].Damage);
    6.        
    7.     }
    Weapons.xml

    Code (xml):
    1. <WeaponCollection>
    2.     <Weapons>
    3.         <Weapon name="a">
    4.             <damage>5</damage>
    5.             <attackspeed>1</attackspeed>
    6.             <range>5</range>
    7.             <IsRanged>false</IsRanged>
    8.         </Weapon>
    9.         <Weapon name="b">
    10.             <damage>3</damage>
    11.             <attackspeed>0.5</attackspeed>
    12.             <range>3</range>
    13.             <IsRanged>false</IsRanged>
    14.         </Weapon>
    15.     </Weapons>
    16. </WeaponCollection>

    I am extremly new to both unity and xml read/write so im having a hard time debugging. Any pointers to good resources would also be appreciated.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    While Debugging you got good start, instead of using XML, have a look into Unity JsonUtility API. And generally Json data format. This will save you lots of headache.
     
  3. Erudaki

    Erudaki

    Joined:
    Apr 26, 2013
    Posts:
    3
    Ill look into it.... but i would also like my question answered with something other than a "Use something else"... thanks.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    You decided to follow 7 years old thread and solutions.
    Not that you can not work with it. Just saying.
    Since then, surely there are better methods to follow.

    Is your fetchWeapons() method prints Debug.Log("Weapons"); in a console?
     
  5. Erudaki

    Erudaki

    Joined:
    Apr 26, 2013
    Posts:
    3
    I did not dismiss your suggestion. Do not mistake me. But a "dont use 'That' use 'This' answer" with almost no explanation as to why its better and no effort to help further the solution is not a good answer.

    Yes, the Debug.Log command within that method does indeed print to the console. however, instead of printing the data expected from the XML, it prints 0.
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    Don't get me wrong, but per week there is few post like yours. So I got option, either not saying anything, or just give you a hint, to save myself on typing n time the same over and over :). In the end, you can use search, to find keyword and more about topic.

    JsonUtility briefly, allows withing few lines of code serialize and deserialize data, of class. Is nice and clean solution.
    https://docs.unity3d.com/ScriptReference/JsonUtility.html

    Try use breakpoint when at runtime, at
    Debug.Log(weaponCollection.Weapons[0].Damage);
    Investigate weaponCollection, in case is null or empty.

    Other option is, serialize you xml to see what data you get, and compare it with what you got in file. Maybe you got invalid syntax there. Same approach you can take with Json.
     
  7. killer_bee_bits

    killer_bee_bits

    Joined:
    May 10, 2018
    Posts:
    4
    This tutorial is pretty informative, you can check it out and see where you are going wrong.