- Home /
Problems with Loading a XML File for a TileMap using XMLTextReader
Im having a Problem loading xml files to load Level data(Tile Map). Im using TextAsset to load xml Files because if I use XMLReader alone the tile map would work but only in the editor not in the standalone build so I'm using TextAsset. Ive tried two solutions both didn't work keeper getting -:
XmlException: Document element did not appear. Line 1, position 1. Mono.Xml2.XmlTextReader.Read () System.Xml.XmlTextReader.Read () Mono.Xml.XmlFilterReader.Read () XMLTileMap.Start () (at Assets/Game_Dir/Scripts/Tech Demo Scripts/XMLTileMap.cs:48) error in unity.
Ill show you two of my Solution and tell me where I'm going wrong and yes i don't yet want to serialise things yet because that area of coding is still a gray area.
Heres the UnityEitor Script Layout:-
Solution 1:-
if(textAsset != null){
Debug.Log("Has Started Parser.....");
//ReadXml from textAsset in Project Folder
StringReader stringReader = new StringReader(textAsset.text);
stringReader.ReadToEnd();
XmlTextReader reader = new XmlTextReader(stringReader);
//While there is data read it
while(reader.Read()){
//When you find a LevelData tag do this
//if(reader.IsStartElement("LevelData")){
//When you Find a WorldSize tag do this
if(reader.Name == "WorldSize"){
WorldWidth = int.Parse(reader.GetAttribute("width"));
WorldHeight = int.Parse(reader.GetAttribute("height"));
Debug.Log("WorldWidth: " + WorldWidth);
Debug.Log("WorldHeight: " + WorldHeight);
}
if(reader.Name == "HasWater"){
string hasWaterString = reader.ReadString();
if(hasWaterString == "true"){
HasWater = true;
Debug.Log("Has Water: " + HasWater);
}
else if(hasWaterString == "false"){
HasWater = false;
Debug.Log("Has Water: " + HasWater);
}
}
if(reader.Name == "TileLayerData"){
TileLayerID = int.Parse(reader.GetAttribute("id"));
string TileDataURL = reader.ReadString();
TileArray = Load(Application.dataPath + "/Game_Dir/Levels/LevelData/" + TileDataURL);
BuildMap();
}
}
}
Solution 2:-
//ReadXml from TileMapFileDir in Project Folder (Original Line of code that only Work in Unity Editor)
//XmlReader reader = XmlReader.Create(Application.dataPath + "/Game_Dir/Levels/LevelData/" + TileMapFileDir);
//ReadXml from TextAsset in Project Folder (New Chunk of code that doesn't fully work yet Gives me the error no Doc Elemement Error)
StringReader textStream = new StringReader(textAsset.text);
textStream.ReadToEnd();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = false;
settings.IgnoreWhitespace = true;
XmlTextReader xtr = new XmlTextReader(textStream);
XmlReader reader = XmlReader.Create(xtr, settings);
//While there is data read it
while(reader.Read()){
//When you find a LevelData tag do this
//if(reader.IsStartElement("LevelData")){
//When you Find a WorldSize tag do this
if(reader.IsStartElement("WorldSize")){
WorldWidth = int.Parse(reader.GetAttribute("width"));
WorldHeight = int.Parse(reader.GetAttribute("height"));
Debug.Log("WorldWidth: " + WorldWidth);
Debug.Log("WorldHeight: " + WorldHeight);
//TileArray = new [WorldWidth, WorldHeight];
}
if(reader.IsStartElement("HasWater")){
string hasWaterString = reader.ReadString();
if(hasWaterString == "true"){
HasWater = true;
Debug.Log("Has Water: " + HasWater);
}
else if(hasWaterString == "false"){
HasWater = false;
Debug.Log("Has Water: " + HasWater);
}
}
if(reader.IsStartElement("TileLayerData")){
TileLayerID = int.Parse(reader.GetAttribute("id"));
string TileDataURL = reader.ReadString();
TileArray = Load(Application.dataPath + "/Game_Dir/Levels/LevelData/" + TileDataURL);
BuildMap();
}
}
Be look on google for hours trying to find the problem with no luck.
Answer by Jeff-Kesselman · May 19, 2014 at 10:52 PM
Why are you starting with a ReadToEnd in line 6? That is going to put your file pointer at the end of the file.
I was look around the on google and it was a suggestion so i tried .readtoEnd() still did work if i delete that line it still gave me a similar Error
Your answer
Follow this Question
Related Questions
Dtd validation with TextAsset xml gives exception 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to edit XML assets at runtime? 1 Answer
How do I use TextAssets for my XML file? 0 Answers