- Home /
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:
<WaveList>
<Level levelId = "1">
<Wave waveId = "1">
<Enemy enemyId = "1"> 5 </Enemy>
<Enemy enemyId = "2"> 0 </Enemy>
<Enemy enemyId = "3"> 0 </Enemy>
</Wave>
</Level>
</WaveList>
And I have a class scheme like this (in XML reader code):
[XmlRoot ("WaveList")]
public class WaveList
{
[XmlElement("Level")]
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")]
[XmlElement("Enemy")]
public List<Enemy> Enemies = new List<Enemy>();
}
public class Level
{
[XmlAttribute ("levelId")]
public int Id;
[XmlElement("Wave")]
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!
Answer by Bunny83 · Jun 08, 2015 at 05:22 AM
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];
Thank you for your clear explanation but, i actually don't know how to deseralize and initialize an xml file. Can you give an example or simply forward me to some articles about them?
Uhm, from the way the question is written i thought you already using something like the XmlSerializer class. You talked about your "X$$anonymous$$L reader code" in your question. Where do you read the X$$anonymous$$L?
You might want to have a look at this $$anonymous$$SDN page about X$$anonymous$$L serialization a few pages down there are examples with custom classes.
Your probably need to put the XmlTextAttribute on your Amount field. Otherwise the way you have your classes setup at the moment the serializer expects an Enemy to look like this:
<Enemy enemyId = "1"> <Amount> 5 </Amount> </Enemy>
So your Enemy should look like this:
public class Enemy
{
[XmlAttribute ("enemyId")]
public int Id;
[XmlText]
public int Amount;
}
See this SO question about the XmlTextAttribute.
I solved it. Thank you man!
By the way, for those need a solution out there, here is the function i used to read and deserialize X$$anonymous$$L:
public static WaveList Load(string path)
{
var serializer = new XmlSerializer(typeof(WaveList));
using(var stream = new FileStream(path, File$$anonymous$$ode.Open))
{
return serializer.Deserialize(stream) as WaveList;
}
}
Usage:
WaveList _WaveList;
string fullPath = Path.Combine(Application.dataPath, "X$$anonymous$$L/WaveList.xml");
_WaveList = WaveList.Load(fullPath);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Loading Xml file issues 0 Answers
xml parsing error 0 Answers