I know, there are tons of Post for this topic, but since I read two days during the posts and nothing worked, I decided to ask here.
I have a XML (Level01.xml) file like this
<map version="1.0">
<tileset>
<image source="....>
</tileset>
<layer name="background">
<data encoding="csv">
3,3,3,3,3,3,3,
3,3,3,3,3,3,3,
3,3,3,3,3,3,3,
3,3,3,3,3,3,3
</data>
</layer>
<layer name="walls">
<data encoding="csv">
182,182,182,182,182,8,8,
182,8,182,8,8,8,8,
182,182,182,182,182,8,8,
182,8,182,8,8,8,8,
</data>
</layer>
</map>
Its a (tile-)map with different layers and I want to draw it in XNA. Therefore I want to read the data from each layer into a string[]
This is what I tried, but it doesn’t work for the second string[] walldata and I am absolutely desperate because I don’t understand why
public class LevelXmlReader
{
public string[] backgroundData;
public string[] wallData;
LevelXmlReader()
{
XDocument doc = XDocument.Load(@"Level/Level01.xml");
foreach (XElement layer in doc.Element("map").Descendants("layer"))
{
var lay = doc.Element("map").Element("layer");
var layName = lay.Attribute("name").Value;
switch (layName)
{
case "background":
{
backgroundData = lay.Element("data").Value.Split(',');
}
break;
case "walls":
{
wallData = lay.Element("data").Value.Split(',');
}
break;
}
}
}
}
When I try to draw wallData, there is always the ExceptionError “Object reference not set to an instance of an object.”
inside your loop you are declaring
this is unecessary, because you already HAVE the individual layer. you need to remove that line entirely and use the
layervariable from the foreach loop. The inside of the loop should now look like thisIn your code, you’re always grabbing the same layer both times, instead of stepping through them.