Hi all I have attempted to write a simple xml reader but have found that it unintentionally skips every other element in the xml file.
Im guessing I am telling it to move onto the next element twice but I am unsure how whats happening or what the solution is.
any help will be greatly appreciated :)
here is a sample of the code and a sample of the xml file
public LevelLoader(string theLevelFile ,ContentManager theContent)
{
XmlTextReader reader = new XmlTextReader(theLevelFile);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "tilerow":
{
currentRow = currentRow + 1;
Console.WriteLine("row found");
currentCol = 0;
break;
}
case "tilecol":
{
Console.WriteLine("col found");
currentTexture = reader.ReadElementContentAsFloat();
currentCol = currentCol + 1;
break;
}
}
}
}
}
sample xml
<tilerow>
<tilecol>1</tilecol><tilecol>2</tilecol><tilecol>3</tilecol><tilecol>4</tilecol><tilecol>5</tilecol><tilecol>6</tilecol><tilecol>7</tilecol><tilecol>8</tilecol><tilecol>9</tilecol><tilecol>10</tilecol>
</tilerow>
The Read() method will first return the Column element, then the Column "Text" then the EndElement. When you use ReadElementContentAsFloat, it reads the current content, then positions the next Read() to the next "Text" section. Since your loop is skipping the Text and the EndElement, it misses 2, 4, ...
Try this instead...
case "tilecol":
{
Console.WriteLine("col found");
reader.Read();
float.TryParse(reader.Value, out currentTexture);
currentCol = currentCol + 1;
break;
}