Unity study notes–data persistence XML file (1)
Unity study notes–data persistence XML file (1) XML related Xml is Extensible Markup Language, a file format. We use xml to complete the persistent storage of data. After we wait for a program to finish running, save the data in the memory (saved on the hard disk/server) to achieve persistent storage of the data. Reading, saving and modifying xml files Key points: Loading of XML files Search and access XML file nodes Reading the content of XML file nodes (InnerText or Attributes[“id”].Value form access) There are detailed comments in the code! Available for reference and comparison learning! using System.IO; using System.Xml; using UnityEngine; namespace Building.XML { public class LoadXMLFile:MonoBehaviour { private void Start() { //Get xml file XmlDocument xmlFile = new XmlDocument(); //Load the text format and parse it into xml form for acquisition. //TextAsset textAsset = Resources.Load(“Text”); // xmlFile.LoadXml(textAsset.text); //Load via path xmlFile.Load(Application.streamingAssetsPath+”/Text.xml”); //Read nodes in xml XmlNode root = xmlFile.SelectSingleNode(“PlayerInfo”); XmlNode nodeName = root.SelectSingleNode(“Name”); XmlNode nodeList = root.SelectSingleNode(“Item”); //Get the data structure type of the customized version print(nodeList.Attributes[“id”].Value); print(nodeList.Attributes[“size”].Value); //or print(nodeList.Attributes.GetNamedItem(“id”).Value); print(nodeList.Attributes.GetNamedItem(“size”).Value); //Get the elements in the array directly XmlNode tempNodeList1 = root.SelectSingleNode(“ItemList1”); XmlNodeList xmlNodeList1 = tempNodeList1.SelectNodes(“Item”); //Find all the nodes in the List and print the InnerText…