classic error CS0266 - struggling to work out the fix.

Hi all

Im getting the classic error:-

I know normally you put (int) / (string) / (GameObject) etcto create the cast but this time I just don’t understand how to fix it…

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

public class xmltest1 : MonoBehaviour {
   
    public class ShipCollection
    {
        public Ship[] Ships;
    }
   
    public class Ship
    {
        [XmlAttribute ("id")]
        public string Shipname;
    }
   
   
    public static void XML(){
        FileStream fs = new FileStream("data.xml", FileMode.Open);
        ShipCollection ships;
        XmlSerializer serial = new XmlSerializer(typeof(ShipCollection));
        ships = serial.Deserialize(fs); //This line causes the error
    }
   
}

The declarations at the top which ones do I actually need to just do some XML testing…Ive managed to confuse myself abit after trolling through forums/examples etc etc :frowning:

Cheers for any help.

Regards,

John

You need to type cast

ships = serial.Deserialize(fs) as ShipCollection;

Darn it…cheers! :slight_smile:

Regards,
John

1 Like