Need Help With XML Reading Class Initialization

Edit: I hope to end up with something like this:

int EnemyId = Levels[1].Waves[3].Enemies[5].Id;
int SpawnAmount = Levels[1].Waves[3].Enemies[5].Amount;

Hi guys, I have an XML scheme like this:

	<Level levelId = "1">
		<Wave waveId = "1">
			<Enemy enemyId = "1"> 5 </Enemy>
			<Enemy enemyId = "2"> 0 </Enemy>
			<Enemy enemyId = "3"> 0 </Enemy>

And I have a class scheme like this (in XML reader code):

    [XmlRoot ("WaveList")]
    public class WaveList
        public List<Level> Levels = new List<Level>();

        public class Enemy
            [XmlAttribute ("enemyId")]
            public int Id;

            public int Amount;

        public class Wave
            [XmlAttribute ("waveId")]
            public int Id;

            //[XmlArray ("Wave"), XmlArrayItem("Enemy")]
            public List<Enemy> Enemies = new List<Enemy>();

        public class Level
            [XmlAttribute ("levelId")]
            public int Id;

            public List<Wave> Waves = new List<Wave>();

But when i try to call like this:

        WaveList.Wave waveToSpawn = new WaveList.Wave();
        waveToSpawn = WaveList.Levels[curLevel].Waves[idOfWaveToSpawn];

It gives me a non-static reference error. And i tried with many other combinations and faced different errors. Can anybody give me a tip about how to fetch a wave and enemies in that wave from this code?

If my code is rubbish, you may suggest a new code scheme too, of course.

Thanks in advance!

Well, you haven’t included where you actually deserialize your XML file… At some point you have to actually read and deserialize your file and as a result you get an instance of your WaveList class. Since all your fields are instance fields (which they have to be or they can’t be serialized) you need an instance of your class.

Your two lines of code both make not much sense:

WaveList.Wave waveToSpawn = new WaveList.Wave();
waveToSpawn = WaveList.Levels[curLevel].Waves[idOfWaveToSpawn];

You create a new instance of your “Wave” class and in the next line you want to replace it with an instance of your Waves List. It’s pointless to create that instance in the first place.

Now to your actual problem. The “Levels” List (declared in line 5 of your WaveList class) is an instance variable of that class but here: WaveList.Levels you try to use it like a static variable. You have to use your deserialized instance instead of the class name

WaveList waveToSpawn;

// waveToSpawn has to be initiallized / deserialized before you can use it below

waveToSpawn = waveListInstance.Levels[curLevel].Waves[idOfWaveToSpawn];