Loading with XML

After following several tutorials I almost have my code working just the way I want, however when I call the load function it doesn’t appear to be correctly parsing the XML document (even though it acquires it) and returns a container of 0 items. Can anyone suggest how to amend my code?

Thanks!

The saving function works, but the load function returns a LeaderContainer of size 0 no matter what document it acquires.

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

[XmlRoot("leaders")]
public class LeaderContainer {

    [XmlArray("Leaders"), XmlArrayItem("Leader")]
    public List<MemoryManager.Leader> leaders = new List<MemoryManager.Leader>();


    public void Save(string path)
    {
        var serializer = new XmlSerializer(typeof(LeaderContainer));

        using (var stream = new FileStream(path, FileMode.Create))
        {
            serializer.Serialize(stream, this);
        }
    }


    public static LeaderContainer Load(string path)
    {
        Debug.Log("Loading " + path);
        TextAsset _xml = Resources.Load<TextAsset>(path);

        XmlSerializer serializer = new XmlSerializer(typeof(LeaderContainer));

        StringReader reader = new StringReader(_xml.text);

        LeaderContainer l = serializer.Deserialize(reader) as LeaderContainer;

        reader.Close();

        Debug.Log(l.rosterSize());

        return l;
    }

    public static LeaderContainer LoadFromText(string text)
    {
        var serializer = new XmlSerializer(typeof(LeaderContainer));
        return serializer.Deserialize(new StringReader(text)) as LeaderContainer;
    }

    public int rosterSize()
    {
        int size = 0;
        foreach (MemoryManager.Leader l in leaders)
        {
            size++;
        }
        return size;
    }

}

Would you please post the XML you expect this to load?

<?xml version="1.0" encoding="shift_jis"?>
<leaders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Leaders>
    <Leader leaderName="Chimaya">
      <description>A red elf</description>
      <cunning>50</cunning>
      <prowess>50</prowess>
      <warmth>50</warmth>
      <stamina>50</stamina>
      <health>50</health>
      <beastCorruption>0</beastCorruption>
      <skin>red</skin>
      <hair>dark</hair>
      <eyes>black</eyes>
    </Leader>
    <Leader leaderName="Freya">
      <description>A grey elf</description>
      <cunning>50</cunning>
      <prowess>50</prowess>
      <warmth>50</warmth>
      <stamina>50</stamina>
      <health>50</health>
      <beastCorruption>50</beastCorruption>
      <skin>red</skin>
      <hair>black</hair>
      <eyes>hazel</eyes>
    </Leader>
  </Leaders>
</leaders>

This is as it was written by the serializer.

Any errors or just the empty list?
Btw, you don’t need to count the items in a list, just use the Count property.

Anyway, first of all I’d make sure you use the correct loading/saving pendants.

Resources.Load takes a relative path to any resource folder in the Assets folder. You use this to load the data from a TextAsset.

Your save function entirely ignores this and uses a FileStream, and you probably pass the absolute path to the resource i guess? Maybe there’s a small typo or something similar? Are you even 100% sure you save/load to the same file?

I’m not saying you could get this to work, but you should definitely decide for one consistent way of saving/loading as this is rather unusual.

Other than that, this looks as if it should work (from the very first glance).