- Home /
Unity Serialization with XML Root problem: 'Does Not Denote Valid Type'
Hi guys,
I've been using Unity for a while now, but I'm only recently digging into XML files in order to save a list of objects. I'm following this tutorial:
http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer
And I followed it to a T, but when I attempt to write to a file I get the following error: Assets/Scripts/ChickenClass.js(23,49): BCE0018: The name 'ChickenCollection' does not denote a valid type ('not found').
All I want to do is save an object and its paramaters as an element of a list of objects in a file, then read it later.
Here's my code:
import System.Collections.Generic;
import System.Xml.Serialization;
import System.Xml;
import System.IO;
@XmlRoot("ChickenCollection")
public class ChickenContainer
{
@XmlArray("Chickens")
@XmlArrayItem("Chicken")
public var Chickens : List.<Chicken>;
public function Save(path : String)
{
var serializer : XmlSerializer = new XmlSerializer(ChickenContainer);
var stream : Stream = new FileStream(path, FileMode.Create);
serializer.Serialize(stream, this);
stream.Close();
}
public static function Load(path : String):ChickenContainer
{
var serializer : XmlSerializer = new XmlSerializer(ChickenContainer);
var stream : Stream = new FileStream(path, FileMode.Open);
var result : ChickenContainer = serializer.Deserialize(stream) as ChickenContainer;
stream.Close();
return result;
}
//Loads the xml directly from the given string. Useful in combination with www.text.
public static function LoadFromText(text : String):ChickenContainer
{
var serializer : XmlSerializer = new XmlSerializer(ChickenContainer);
return serializer.Deserialize(new StringReader(text)) as ChickenContainer;
}
}
And in my class file:
class ChickenClass extends MonoBehaviour{
public class Chicken{
var name : String;
var age : int; // 0 is egg, 1 is chick, 2 is hen
var ageSeconds : float;
// Aesthetic Features
var featherColor : String;
var beakColor : String;
var combColor : String;
var price : int;
var hunger : int;
var box : int;
private function Chicken(){
// Empty Constructor for serialization
}
public function Save(){
// Write self to XML file
var chickenCollection : ChickenCollection = ChickenContianer.Load(Path.Combine(Application.dataPath, "chickens.xml"));
chickenCollection.Save(Path.Combine(Application.persistentDataPath, "chickens.xml"));
}
public function Chicken(chickenName : String, ageState : int, ageSecs : float, color0 : String, color1 : String, color2 : String, priceOfChicken : int, hungerNum : int, boxNum : int){
name = chickenName;
age = ageState;
ageSeconds = ageSecs;
featherColor = color0;
beakColor = color1;
combColor = color2;
hunger = hungerNum;
box = boxNum;
price = priceOfChicken;
}
}
}
Could you please point me in the right direction? I've looked through a lot of docs and I'm not sure where to go from here. My game is nearly functional, but it won't work without this saving feature.
Thank you so much.
Your answer
Follow this Question
Related Questions
ScriptableObject+XmlSerializer=sad 2 Answers
Deserializing data with XML Serializer 0 Answers
Problems with the XML Serializer 1 Answer
"Root Element Missing: XML Exception" when trying to load data from XML file 2 Answers
URGENT - Cannot be serialized because it does not have a default public constructor.... 1 Answer