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 chetanisinanand · Mar 20, 2015 at 11:44 AM · unity5xmlxmlserializer

Getting data from XML

alt text

I've a LevelController script with Serializable Classes containing data required for each level ( see Attached Image) I want to get these data from a nested XML file .

I tried some of the XML parsing however not sure how to get int/float values and I'm not able to format it properly .

Please help me with a c# script to get data from a XML file and assign it in LevelController script

levelcontroller.png (72.0 kB)
Comment
Add comment · Show 1
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 fffMalzbier · Mar 20, 2015 at 12:28 PM 0
Share

Can you add the try you did in your question? The script and a example xml if possible. Then we can try to fix / improve it.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by chetanisinanand · Mar 24, 2015 at 07:28 PM

Thanks Cherno for your great explanation, However using System.XML will add a couple of MBs to your project, Rather I used Lightweight XML parser and figured out how it works ,

I'm putting here my code if this can help someone out there :-) Just download the XML parser from the link and drag all three file in your Assets directory.

This is my XML file I used for demonstration:

 Levels.xml
     
 <LevelsContainer>    
         <Level id ="1" attribute1 ="5" atribute2 ="avoid red dots">
             <NodeA>
                 <NodeB attributeB1 ="1.2" attributeB2 ="-60">
                     <NodeC attributeC1 ="1"></NodeC>
                    </NodeB>
              </NodeA>
         </Level>
     
          <Level id ="2" attributeA ="5" atributeB ="">
             <NodeA>
                 <NodeB attributeB1 ="1.2" attributeB2 ="60">
                     <NodeC attributeC1 ="120"></NodeC>
                    </NodeB>
             </NodeA>
         </Level>
 <LevelsContainer>


And here is the XML Reader Script:

 using UnityEngine;
 using System.Collections;
 
 public class XMLReaderExample : MonoBehaviour
 {
     public     TextAsset LevelXMLFile ;
     // Use this for initialization
     void Start (){
     
         XMLParser parser = new XMLParser();
         XMLNode LevelXML = parser.Parse(LevelXMLFile.text);
 
         int _numberOfLevels = LevelXML.GetNodeList ("LevelsContainer>0>Level").Count;
         for (int i = 0; i < _numberOfLevels; i++) {
 
             string _attribute1ValueString = LevelXML.GetValue ("LevelsContainer>0>Level>" + i + ">@attribute1");
             int _attribute1ValueInt;
             int.TryParse(_attribute1ValueString,out _attribute1ValueInt);
             Debug.Log (_attribute1ValueInt);
             //similarly we can get attribute2 Values, or any other attribute from any other node
 
             int  _nodeACount  = LevelXML.GetNodeList("LevelsContainer>0>Level>" + i + ">NodeA").Count;
             for (int j = 0; j < _nodeACount; j++) {
 
                 int _nodeBCount = LevelXML.GetNodeList("LevelsContainer>0>Level>" + i + ">NodeA>" +j+ ">NodeB").Count;
                 for (int k = 0; k < _nodeBCount; k++) {
 
                     string _attributeB1ValueString =  LevelXML.GetValue ("LevelsContainer>0>Level>" + i + ">NodeA>" +j+ ">NodeB>" +k+ ">@attributeB1");
                     float _attributeB1ValueFloat;
                     float.TryParse (_attributeB1ValueString,out _attributeB1ValueFloat);
                     Debug.Log (_attributeB1ValueFloat);
 
                     XMLNode _nodeC = LevelXML.GetNode ("LevelsContainer>0>Level>" + i + ">NodeA>" +j+ ">NodeB>" +k+ ">NodeC>0");
 //                    Do whatever you want with nodeC values :-)
 
 //                    GetNode("path>0>to>0>array>0>node>0");
 //                    GetNodeList("path>0>to>0>array");
 //                    GetValue("path>0>to>0>array>0>node>0>@attribute");
                 }
             }
         }
     }
 }

Happy Coding :-) Apologies for typing mistakes (if any)

Comment
Add comment · 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
0

Answer by Cherno · Mar 20, 2015 at 02:25 PM

Ok, here goes. I'm gonna use an example from my own project, a weapons class that is fed via XML file.

I recommend "XML Notepad", and be sure to enode to "utf-8".

Suppose you have the Weapon class:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Xml;
 using System.Xml.Serialization;
 
 [System.Serializable]
 public class Weapon {
 
     [XmlIgnore]/*[System.NonSerialized]*/ public GameObject muzzleflashObject;
 
     [XmlAttribute("name")] public string name = "";
     [XmlAttribute("damage")] public int damage = 0;
     [XmlAttribute("zoom_max")] public float zoom_max = 30f;
 
 }


Then, you need a simple class often caleed a directory for this Weapon class, which will hold the Weapon classes which are in turn read from XML:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Xml;
 using System.Xml.Serialization;
 
 [System.Serializable]
 [XmlRoot("weapons")]
 public class WeaponDirectory {
     [XmlElement("weapon")]
     public Weapon[] weapons;
     
     
 }

Now we need a XML file which is organized like this:

 weapons
      +weapon
           name
           damage
           zoom_max
      + weapon
           name
           damage
           zoom_max
      ...
           
           

Now we can read the XML file:

 using System.Xml.Serialization;
 using System.IO;
 using System.Text;
 using System;
 using System.Xml;
 using System.Collections.Generic;
 
 public class ReadFromXML: MonoBehaviour {
 
      public  Dictionary<string,Weapon>() weaponDictionary;
 
      void ReadWeapons() {
           weaponDictionary = new Dictionary<string,Weapon>();
         
            string path = "C:/Weapons.xml";
         
            var xmlSerializer = new XmlSerializer(typeof(WeaponDirectory));
            var stream = File.Open(path, FileMode.Open);
            var deserializedWeapons = xmlSerializer.Deserialize(stream) as WeaponDirectory;
         
            stream.Close();
         
            for(int i = 0; i < deserializedWeapons.weapons.Length; i++) {
                 Weapon weapon = deserializedWeapons.weapons[i];

                 Manager.world.weapons.Add (weapon.name, weapon);
            }
       }
 }
 
 
 
 
Comment
Add comment · 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

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

23 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 avatar image avatar image

Related Questions

DataContract serialization not compatible with Unity? 0 Answers

Serialization and XML 1 Answer

XML Serialization in Windows phone 8 2 Answers

ArgumentException: Path is empty while saving data in an XML 0 Answers

Unity Serialization with XML Root problem: 'Does Not Denote Valid Type' 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