Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • XML Parsing in Unity

    • 0
    • 2
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 1.64k
    Comment on it

    Hello Readers

     

    Today I am going to explain you parsing of XML in unity. XML can be parse in unity by two ways.

    1.       By Class method.

    2.       By Normal method.

     

    Let’s discuss both these one by one.

    1.       By Class Method:-  First of all, create a XML file and put all your fields in that. Like shown below:-

    <?xml version="1.0" encoding="utf-8"?>
    
    <LevelData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    
    <Level>1</Level>
    
    <Color>1</ Color >
    
    </LevelData>

     

    Save this as an XML file.

     

    Now you need to create a class in with same name “LevelData” in the unity as shown below:-

     

    using UnityEngine;
    
    using System.Collections;
    
    using System.Xml.Serialization;
    
    using System.Collections.Generic;
    
    using System;
    
    
    [Serializable]
    
    [XmlRoot("LevelData")]
    
    public class LevelData : MonoBehaviour
    
    {
    
                    [XmlElement("Level")]
    
                    public int LevelNumber;
    
                   [XmlElement("Color")]
    
                   public int Color;
    
    }

     

    Now create one more class to read the data as shown below:-

    void Start()
    {
            string path = Path.Combine(Application.dataPath, "Resources/XMLNAME.xml");
            LevelData data = XmlUtil<LevelData>.LoadDataFromXmlAsset<LevelData>(path);
            Debug.Log(data.LevelNumber);
    
    }

    This is how you can ready data. 

    Keep coding.  

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: