Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ash-Bash32 · May 19, 2014 at 10:41 PM · c#xmltextasset

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:-

UnityEditor 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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ash-Bash32 · May 19, 2014 at 11:09 PM 0
Share

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

avatar image Ash-Bash32 · May 20, 2014 at 03:01 PM 0
Share

Fixed all the Problems

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges