XML serialization error while trying to read data

Hello. Fairly new to unity and was trying to read some data in from a file. I found this Saving and Loading Data: XmlSerializer 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

using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using UnityEngine;

[XmlRoot("WeaponCollection")]
public class WeaponContainer {

    [XmlArray("Weapons"), XmlArrayItem("Weapon")]
    public Weapon[] Weapons;

    public void Save(string path)
    {
        var serializer = new XmlSerializer(typeof(WeaponContainer));
        using (var stream = new FileStream(path, FileMode.Create))
        {
            serializer.Serialize(stream, this);
        }
    }

    public static WeaponContainer Load(string path)
    {
        var serializer = new XmlSerializer(typeof(WeaponContainer));
        using (var stream = new FileStream(path, FileMode.Open))
        {
            return serializer.Deserialize(stream) as WeaponContainer;
        }
    }

}

Weapon.Cs

using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using UnityEngine;

public class Weapon {
    [XmlAttribute("name")]
    public string Name;
    public float Damage;
    public float AttackSpeed;
    public float Range;
    public bool bIsRanged;

    // Use this for initialization
    void Start () {
       
    }

    // Update is called once per frame
    void Update () {
       
    }
}

Load Function in another class

public void fetchWeapons()
    {
        var weaponCollection = WeaponContainer.Load(Path.Combine(Application.dataPath, "XML/Weapons.xml"));
        Debug.Log("Weapons");
        Debug.Log(weaponCollection.Weapons[0].Damage);
       
    }

Weapons.xml

<WeaponCollection>
    <Weapons>
        <Weapon name="a">
            <damage>5</damage>
            <attackspeed>1</attackspeed>
            <range>5</range>
            <IsRanged>false</IsRanged>
        </Weapon>
        <Weapon name="b">
            <damage>3</damage>
            <attackspeed>0.5</attackspeed>
            <range>3</range>
            <IsRanged>false</IsRanged>
        </Weapon>
    </Weapons>
</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.

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.

Ill look into it… but i would also like my question answered with something other than a “Use something else”… thanks.

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?

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.

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.

This tutorial is pretty informative, you can check it out and see where you are going wrong.