Saving xml data

I created a thread about this in unity support and then realized this should be in scripting. Please forgive the double post but I think it will much more likely be answered here.

I’ve followed the tutorial on the unity wiki but am having some trouble actually implementing it. Here is the page I’m reading: http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer I’ve created a class for the individual blocks and currently trying to store the name.

import System.Xml;
import System.Xml.Serialization;

public class Block {

    @XmlAttribute('name')
    public var name : String;

}

And then I’ve built a container like in the wiki - basically the same code except I change ‘Monster’ to ‘Block’

import System.Collections.Generic;
import System.Xml.Serialization;

@XmlRoot('BlockCollection')
public class BlockContainer {

    @XmlArray('Blocks')
    @XmlArrayItem('Block')
    public var Blocks : List.<Block> = new List.<Block>();

    public function Save(path : String) {

	print('saving blocks');

        var serializer : XmlSerializer = new XmlSerializer(BlockContainer);
	var stream : Stream = new FileStream(path, FileMode.Create);
	serializer.Serialize(stream, this);
	stream.Close();

    }

    public static function Load(path : String):BlockContainer {

        var serializer : XmlSerializer = new XmlSerializer(BlockContainer);
	var stream : Stream = new FileStream(path, FileMode.Open);
	var result : BlockContainer = serializer.Deserialize(stream) as BlockContainer;
	stream.Close();
	return result;

    }

    public static function LoadFromText(text : String):BlockContainer {

        var serializer : XmlSerializer = new XmlSerializer(BlockContainer);
	return serializer.Deserialize(new StringReader(text)) as BlockContainer;

    }

}

Both of these classes seem to cause no errors. I’m able to create a new Block object in the code below. However, when I try to add that Block object to the List within BlockContainer, it seems nothing is happening. Likewise, calling the Save function on BlockContainer doesn’t reach the print statement I put in the class and also saves nothing. I’ve manually created the path in Application.persistentDataPath so I know it exists.

function testSaveScene() {

    var path = Application.persistentDataPath + '/testSave.xml';
    
    //var xmlStartData : String = '<BlockCollection><Blocks><Block><name>coreyDrake</name></Block></Blocks></BlockCollection>';
    //var blockCollection : BlockContainer = BlockContainer.LoadFromText(xmlStartData);
    var blockCollection = new BlockContainer();
    print(blockCollection.Blocks);
    var testBlock = new Block();
    testBlock.name = 'corey';
    blockCollection.Blocks.Add(testBlock);
    print(blockCollection.Blocks);
    blockCollection.Save(path);

}

What confuses me most about this is both Block and BlockContainer are just classes built in such a similar way. The fact that I can change things in block, but not blockCollection is weird. I’m using that Add function, and maybe improperly. But what’s weird is that I get no errors.

Has no one done this?..

I’ve narrowed down the problem. In the BlockContainer class, I removed the creation of the list and substituted just a string attribute and it serialized as expected. So it’s something about how I’m creating the list that is the problem. I’ve searched everywhere about this and creating this list is written differently. For example:
public var Blocks : List. = new List.(); ← Causes no syntax error but just won’t work
public var Blocks : List.; ← From another part of that same wiki page - they decide to change this line between the two examples - But I get this error: NullReferenceException: Object reference not set to an instance of an object
public List Blocks = new List();

In this area I’ve followed the example 100 percent right, for both versions of declaring the list. (which seems like it might be an error on the wiki?)

Is it just the serialization that’s failing? Have you checked and verified that your block is actually being added to the list in your block container? Btw… I’ve done lots of XML serialization (outside of Unity) but never with JavaScript so syntactically I’m not much help. Only things I can think is that maybe you don’t have an XmlType attribute specified for your Block class.

You should maybe use the @XmlType(“Block”) attribute at the class declaration level for your “Block” class. Also, in your BlockContainer, right after your @XmlRoot attribute, add the following attribute: @XmlInclude(typeof Block)

My syntax may be a little incorrect… but give it a shot (and let intellisense be your friend in fixing the syntax).

I’ll look into those. But it is indeed failing to add to the list. When I print(blockCollection.Blocks);, I get System.Collections.Generic.List`1[Block] both times

[Edit] I take it back! Items are being put into the List after all. Still, it’s never reaching the Save method anyway, as it’s not printing out that debug statement.

So I think the main question is: What is wrong with the command, blockCollection.Save(path); or what is wrong with the method declaration, public function Save(path : String)

[Edit Again] I totally forgot that earlier I proved that it could serialize properly, that the list was the problem. Sigh I’m going delirious. I’m using an array now, as iOS can’t use it anyway, with the same problem as List