Problem with XmlTextReader - don't read all entries

Hi.
I have same xml file (maps.xml):

<?xml version="1.0" encoding="utf-8" ?>
<maps size="1">
   <map name="Town Namen" entries="4">
	<tile name="0x0" x="0" z="0"></tile>
	<tile name="10x0" x="10" z="0"></tile>
	<tile name="0x10" x="0" z="10"></tile>
	<tile name="10x10" x="10" z="10"></tile>
   </map>
</maps>

And when I want read “tile” i can’t becouse reader read only first and second.

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Xml;
using System.IO;
using System.Text;

[CustomEditor (typeof(TileManager))]
public class TileManagerEditor : Editor 
{
	GameObject[] maps;
	int mapsCount;
	GameObject tile;
	GameObject ground_001;

	public override void OnInspectorGUI()
	{
		ground_001 = EditorGUILayout.ObjectField("Ground 001", ground_001, typeof(GameObject)) as GameObject;
		if(GUILayout.Button("Load"))
		{
			TextAsset assets = (TextAsset)Resources.Load("maps", typeof(TextAsset));
			XmlTextReader reader = new XmlTextReader(new StringReader(assets.text));
			reader.Read();
			int i = 0;
			int entries = 0;
			int maxEntries = 0;
			while(reader.Read())
			{
				if(reader.IsStartElement("maps"))
				{
					Debug.Log("Size:" + reader.GetAttribute("size"));
					mapsCount =  int.Parse(reader.GetAttribute("size"));
					maps = new GameObject[mapsCount];
				}

				if(reader.IsStartElement("map"))
				{
					Debug.Log("Name:" + reader.GetAttribute("name"));
					maps[i] = new GameObject (reader.GetAttribute("name"));
					maps[i].transform.parent = GameObject.Find("Tiles").transform;
					
					maxEntries = int.Parse(reader.GetAttribute("entries"));
					for(entries = 0; entries < maxEntries; entries++)
					{
						reader.Read();
						if(reader.IsStartElement("tile"))
						{
							Debug.Log(reader.GetAttribute("name"));
							int xx = int.Parse(reader.GetAttribute("x"));
							int zz = int.Parse(reader.GetAttribute("z"));
							tile = Instantiate(ground_001, new Vector3(xx, 0, zz), Quaternion.identity) as GameObject;
							tile.transform.parent = maps[i].transform;
							tile.name = reader.GetAttribute("name");
						}
					}
					maxEntries = 0;
					i++;
				}
				
			}
		}
	}
}

I’m guessing because you need more than 4 calls to reader.Read(). Try this instead:

int entries = 0; 
while (entries < maxEntries)
{
	reader.Read();
	if(reader.IsStartElement("tile"))
	{
		entries++;
		Debug.Log(reader.GetAttribute("name"));
		int xx = int.Parse(reader.GetAttribute("x"));
		int zz = int.Parse(reader.GetAttribute("z"));
		tile = Instantiate(ground_001, new Vector3(xx, 0, zz), Quaternion.identity) as GameObject;
		tile.transform.parent = maps[i].transform;
		tile.name = reader.GetAttribute("name");
	}
}

EDIT: I just realized that code might go into an infinite loop if your max “entries” count is greater than the number of actually defined tiles. Perhaps do a check for reader.EOF as well.

It’s work. Thank you very much