Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Cyrax5710 · Mar 01, 2015 at 01:24 AM · tilemaploadinggridxmlxmlserializer

I'm trying to deserialize an XML file with no success

I have the following XML file:

  <world>
         <tile x="-2" y="0" z="4">
             <item id="3" />
         </tile>
         <tile x="-3" y="0" z="4">
             <item id="3" />
         </tile>
     </world>

world encompasses and array of tiles and each tile encompasses an array of items (not shown in this file).

I'm using the following function to deserialize the file:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Xml;
 using System.Xml.Serialization;
 using System.IO;
 using System.Text;
 
 [XmlRoot("world")]
 public class WorldContainer
 {
     [XmlArray("tile")]
     public Tile[] tiles;
     
     //load XML file
     public static WorldContainer Load(string path)
     {
         var serializer = new XmlSerializer(typeof(WorldContainer));
         using(var stream = new FileStream(path, FileMode.Open))
         {
             return serializer.Deserialize(stream) as WorldContainer;
         }
     }
     

Here are the classes involved: Tile class

 using UnityEngine;
 using System.Collections;
 using System.Linq;
 using System.Collections.Generic;
 using System.Xml;
 using System.Xml.Serialization;
 
 //Holds the stack and properties of each point in the game world
 public class Tile {
 
     [XmlArray("item")]
     public List<Item> stack;
 
     [XmlAttribute("x")]
     public int x;
     [XmlAttribute("y")]
     public int y;
     [XmlAttribute("z")]
     public int z;
 
     public Tile()
     {
         //stack = new List<Item> ();
     }
 
     public Tile(int X, int Y, int Z)
     {
         x = X;
         y = Y;
         z = Z;
         stack = new List<Item> ();
     }
 
     // Update is called once per frame
     void Update () {
     
     }
 }


Item class

 using UnityEngine;
 using System.Collections;
 using System.Xml;
 using System.Xml.Serialization;
 
 public class Item {
 
     [XmlAttribute("id")]
     public int id;
 
     [XmlIgnore]
     public GameObject item;
     [XmlIgnore]
     public int stackPos;
     [XmlIgnore]
     public int y;
 
     public Item()
     {
     }
 
     public Item(int Id, GameObject x, int stackpos, int yvalue)
     {
         id = Id;
         item = x;
         stackPos = stackpos;
         int y = yvalue;
         //typo?
     }
 
     public Item (Item copy)
     {
         id = copy.id;
         item = copy.item;
         stackPos = copy.stackPos;
         y = copy.y;
     }
 
     public Item(GameObject x)
     {
         item = x;
         worldObject properties = x.GetComponent<worldObject>();
         stackPos = properties.stackPosType;
         y = GUI.SelectedLevel;
     }
 }

Once I try to load and deserialize the file I do this:

 var grid = WorldContainer.Load (Path.Combine (Application.dataPath, "Resources/XMLFiles/" + fileName));
 
         
         //iterate through each tile and item read from the XML file
         int i = 0, j = 0;
         foreach(Tile tile in grid.tiles)
         {
             Debug.Log ("Tile "+i+": coordinates ("+tile.x+", "+tile.y+", "+tile.z+")");
             foreach(Item item in tile.stack)
             {
                 Debug.Log ("Item "+j+" id= "+item.id);
                 j++;
             }
             i++;
         }
         Debug.Log ("Finished reading...");
         
     }


It comes back with nothing, it only returns one tile and the coordinates are 0,0,0 which is not true if you look at the XML file.

I haven't had much luck dealing with XML, I've found articles and examples but they are confusing and I cannot figure out what is wrong.

Any input is appreciated, thanks in advance

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
1
Best Answer

Answer by dorpeleg · Mar 01, 2015 at 09:17 AM

Try using XmlElement instead of XmlArray. like so:

  [XmlElement("tile")]
  public Tile[] tiles;

and the Tile class:

 [XmlRoot("tile")]
 public class Tile
 {
     [XmlAttribute("x")]
     public int x;
 
     [XmlAttribute("y")]
     public int y;
 
     [XmlAttribute("z")]
     public int z;
 
     [XmlElement("item")]
     public Item[] items;
 }

Item class:

 [XmlRoot("item")]
 public class Item
 {
     [XmlAttribute("id")]
     public int id;
 }

I remember having issues with XmlArray myself.

Comment
Add comment · Show 1 · 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 Cyrax5710 · Mar 01, 2015 at 08:35 PM 0
Share

@dorpeleg Thanks, it worked. Now the values I'm getting are what they're supposed to be

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

TBS - Suggestion (Beginner) 0 Answers

Change hex tilemap to use axial coordinates? 0 Answers

Load file on Android 1 Answer

Parse XML to Vector3 (and other objects you don't control) 0 Answers

WebGL XML parsing error "InvalidOperationException" 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