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
    • 493
    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;
    public class LevelData
    	public int LevelNumber;
    	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();
        public void loadData(int i){
    		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

Sign up using

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: