大家好, 我试图编写一个简单的XML读取器,但发现它无意中跳过了每个XML文件中的其他元素。
我猜我正在告诉它两次移动到下一个元素,但我不确定发生了什么或解决方案是什么。
任何帮助将不胜感激:)
以下是代码示例和XML文件示例:
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;
}
}
}
}
}
示例 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>