Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Load and save data from xml

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 520
    Comment on it

    Hello guys,

    I have faced some problem while loading data from xml, while loading I was doing some simple mistakes, So I thought the share the correct process with you so that if in any case, anyone faces issues they can get help from here easily

    For loading data properly from xml we need some simple code to follow.

    I have attached XmlUtil file which will help you load your data as well as you can save your data in xml.

    LevelData class is a class in which you have all the attributes of your xml data.
    
    using UnityEngine;
    using System.Collections;
    using System.Xml.Serialization;
    using System.Collections.Generic;
    using System;
    
    [Serializable]
    
    [XmlRoot("LevelData")]
    
    public class LevelData
    {
    	[XmlElement("Level")]
    	public int LevelNumber;
    
    	[XmlElement("Rows")]
    	public int rows;
    }
    
    
    Second you have your main class in which you will do loading and saving data by calling xmlutil methods.
    
    Add this line of code in your main class
    
    private LevelData levelData = null;
    
    	void Start()
    	{
    		levelData = new LevelData();
    		loadData(level);
    
    	}
    
        public void loadData(int i){
    		Debug.Log("loaddata");
    		string path = "LevelData/Data_"+i.ToString();
    		levelData = XmlUtil<LevelData>.LoadDataFromXmlAsset<LevelData>(path);
    		Debug.Log("load " + levelData.LevelNumber);
    	}
    

    Here XmlUtil<LevelData>.LoadDataFromXmlAsset<LevelData>(path); will call Xmlutil method with path of xml data file. By which, you will get all your level info in levelData. And then you can use its element according to your own way.

    I have attached two files

    1. XmlUtil  // file is necessary , you can also copy the methods inside it and use in your class.

    2. levedata xml file // demo files just to have the lookup of xml file.

    So Implement it and enjoy , If you find any problem please comment.

 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: