Xml and json are both common data storage methods. Save them first and forget to use them directly...
Using System. IO; using System. text; using System. xml; using UnityEngine; using System. collections; public class XMLApply: MonoBehaviour {public string fileName = "Books. xml "; private void Start () {// CreateXML (); // UpdateXml (); // AddXml (); ParseXML ();}////// Create an XML file ///Public void CreateXML () {string filePath = Application. dataPath + "./" + fileName; if (! File. exists (filePath) {XmlDocument BooksXML = new XmlDocument (); XmlElement myFavoriteBooks = BooksXML. createElement ("MyFavoriteBooks"); XmlElement history = BooksXML. createElement ("History"); history. setAttribute ("year", "2013"); XmlElement item_empireOfQing = BooksXML. createElement ("item"); item_empireOfQing.SetAttribute ("name", ""); item_empireOfQing.SetAttribute ("price", "100"); item_empireOfQing.InnerText = "Sara "; xmlElement item_theLastMan = BooksXML. createElement ("item"); item_theLastMan.SetAttribute ("name", "Catcher in the wheat field"); item_theLastMan.SetAttribute ("price", "70"); item_theLastMan.InnerText = "Lisa "; xmlElement item_worldHistory = BooksXML. createElement ("item"); item_worldHistory.SetAttribute ("name", "World History"); item_worldHistory.SetAttribute ("price", "40"); item_worldHistory.InnerText = "Michael "; xmlElement software = BooksXML. createElement ("SOftware"); XmlElement item_php = BooksXML. createElement ("PHP"); item_php.SetAttribute ("name", "php"); item_php.SetAttribute ("price", "35"); item_php.InnerText = "KD"; history. appendChild (item_empireOfQing); history. appendChild (item_theLastMan); history. appendChild (item_worldHistory); software. appendChild (item_php); myFavoriteBooks. appendChild (history); myFavoriteBooks. appendChild (software); BooksXML. appendChild (myFavoriteBooks); BooksXML. save (filePath); Debug. log ("XML file create success ..... ");}}////// Update the xml file content ///Public void UpdateXml () {string filePath = Application. dataPath + "/" + fileName; if (File. exists (filePath) {// create a xml reference XmlDocument BooksXML = new XmlDocument (); // read exists file into BooksXML. load (filePath); XmlNodeList nodeList = BooksXML. selectSingleNode ("MyFavoriteBooks "). childNodes; foreach (XmlElement node in nodeList) {if (node. getAttribute ("year") = "2013") {node. setAttribute ("year", "2014"); XmlNodeList subNodeList = node. childNodes; foreach (XmlElement subNode in subNodeList) {subNode. innerText + = "Updated... ";} break ;}} BooksXML. save (filePath); Debug. log ("Update success... ");}}////// Add a node to the existing xml file ///Public void AddXml () {string filePath = Application. dataPath + "/" + fileName; if (File. exists (filePath) {XmlDocument BooksXML = new XmlDocument (); BooksXML. load (filePath); XmlNode root = BooksXML. selectSingleNode ("MyFavoriteBooks"); XmlElement culture = BooksXML. createElement ("Culture"); culture. setAttribute ("year", "2012"); XmlElement item_China = BooksXML. createElement ("item"); item_China.SetAttribute ("name", "Chinese culture"); item_China.SetAttribute ("price", "30"); item_China.InnerText = "rechard"; culture. appendChild (item_China); root. appendChild (culture); BooksXML. appendChild (root); BooksXML. save (filePath); Debug. log ("Add node success... ");}}////// Parse the xml file ///Public void ParseXML () {string filePath = Application. dataPath + "/" + fileName; StringBuilder booksInfo = new StringBuilder (""); XmlDocument BooksXML = new XmlDocument (); BooksXML. load (filePath); XmlNode rootNode = BooksXML. firstChild; if (rootNode. name = "MyFavoriteBooks") {XmlNodeList nodeList = rootNode. childNodes; foreach (XmlElement node in nodeList) {booksInfo. append (node. name + "\ n"); foreach (XmlElement subNode in node. selectNodes ("item") {booksInfo. append ("\ t" + subNode. getAttribute ("name") + ", price:" + subNode. getAttribute ("price") + "\ n");} booksInfo. append ("\ n");} Debug. log (booksInfo. toString ();} else {Debug. log ("you load wrong file .... ");}}}
The following figure is attached: