Unity3d reads XML documents

  1. I have read the unity3d reading XML summarized by many people before, and there are all kinds of them. Today, on a whim, I also summarize an article for your reference. If there is any inappropriateness, you are welcome to leave a message to discuss. Let’s not talk nonsense, let’s get to the point. Let's make a rendering first

  1. To write xnl documents, in order to facilitate the direct conversion and display of various data, I deliberately collected 5 different data types for your reference
  2. Create a new empty object LoadXml, write the LoadingXML script, and read the file

The following is the whole process of reading, encapsulating the data that needs to be called, and providing get and set methods to facilitate external calls

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using System.IO;

using System.Xml;

 

public class LoadingXML : MonoBehaviour

{

    public static LoadingXML instance;

    private string xmlfilePath;

 

    private int id;

    private string name;

    private byte age;

    private char sex;

    private float grade;

 

    public int Id { get => id; set => id = value; }

    public string Name { get => name; set => name = value; }

    public byte Age { get => age; set => age = value; }

    public char Sex { get => sex; set => sex = value; }

    public float Grade { get => grade; set => grade = value; }

 

    void Awake ()

    {

        instance = this;

        / / Declare the path of the xml file

        xmlfilePath = Application.streamingAssetsPath + "/database.xml";

        if (File.Exists(xmlfilePath))

        {

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(xmlfilePath);

            // get the root node

            XmlNode xn = xmlDoc.SelectSingleNode("students");

 

            // Get all child nodes of the root node

            XmlNodeList xnl = xn.ChildNodes;

 

            // Convert the node to an element to get the attribute value of the node

            foreach (XmlNode xn1 in xnl)

            {

                // Convert the node to an element to get the attribute value of the node

                XmlElement vehicle = (XmlElement)xn1;

                // Convert the read data to int

                Id = int.Parse(xe.GetAttribute("Id"));

                Name = xe.GetAttribute("Name").ToString();

                // Get the child nodes of the book node

                XmlNodeList xnIO = xe.ChildNodes;

                // Convert the read data into byte

                Age = byte .Parse(xnIO.Item(0).InnerText);

                // Convert the read data into char

                Sex = char .Parse(xnIO.Item(1).InnerText);

                // Convert the read data to float

                Grade = float .Parse(xnIO.Item(2).InnerText);

 

            }

        }

        else {

            Debug.Log("weifaxian");

        }

    }

5. Create 5 texts in unity3d for displaying text, as follows:

6. Create a new empty object ManagerUI, and write the script LoadUIData , and directly upload the script below

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

 

public class LoadUIData : MonoBehaviour

{

    private Text Tx_01;

    private Text Tx_02;

    private Text Tx_03;

    private Text Tx_04;

    private Text Tx_05;

    // Start is called before the first frame update

    void Start()

    {

        Tx_01 = GameObject.Find("Canvas/Tx_01").GetComponent<Text>();

        Tx_02 = GameObject.Find("Canvas/Tx_02").GetComponent<Text>();

        Tx_03 = GameObject.Find("Canvas/Tx_03").GetComponent<Text>();

        Tx_04 = GameObject.Find("Canvas/Tx_04").GetComponent<Text>();

        Tx_05 = GameObject.Find("Canvas/Tx_05").GetComponent<Text>();

        LoadData();

    }

    void LoadData()

    {

        //int converted to string

        Tx_01.text = (LoadingXML.instance.Id).ToString();

        Tx_02.text = LoadingXML.instance.Name;

        //byte converted to string

        Tx_03.text = (LoadingXML.instance.Age).ToString();

        //char converted to string

        Tx_04.text = (LoadingXML.instance.Sex).ToString();

        //float converted to string

        Tx_05.text = (LoadingXML.instance.Grade).ToString();

}

7. Run to see the effect

Guess you like

Origin blog.csdn.net/m0_38116456/article/details/104043685