Unity uses C # to parse and update TXT, Json and XML files
preparation
public class Site { public string Name{get; set;} public string Url{ get; set;} }
public class SitesList { public Site[] sites; }
//Application.dataPath, the attribute used to obtain the assets path in unity, is only used for example testing. string path = Application.dataPath + "/Resources/"; string jsonName = "Sites.json"; string txtName = "Sites.txt"; string xmlName = "Sites.xml"; string jsonPath = path + jsonName; string txtPath = path + txtName; string xmlPath = path + xmlName;
TXT file storage and parsing
TXT save
public void TxtToFile(SitesList sitesList,string filePath) { string str = "Name|Url" + '\n'; for (int i = 0; i < sitesList.sites.Length; i++) { str += sitesList.sites[i]. Name + '|' + sitesList.sites[i]. Url //The last line does not need additional line breaks + (i == sitesList.sites.Length - 1 ? "": "\n"); } FileStream fs = File.Open (filePath, FileMode.Create); StreamWriter sw = new StreamWriter (fs); sw.Write (str); sw.Flush (); sw.Close (); fs.Close (); Debug.Log ("saved successfully"); }
Site s1=new Site() {Name="Coming and going", Url=“ http://www.azimiao.com "}; Site s2 = new Site (){ Name = "Xema", Url = "https://" }; Site s3 = new Site (){ Name ="Pret",Url = "https://"}; SitesList siteList = new SitesList (); siteList.sites = new Site[]{ s1,s2,s3}; TxtToFile (siteList, txtPath);
Name|Url Zimiao haunts| http://www.azimiao.com Xema|https:// Pret|https://
TXT reading and formatting
public void TxtToClass(out SitesList rec,string filePath) { rec = new SitesList (); if (! File.Exists(filePath)) { Debug.Log ("File does not exist"); return; } FileStream fs = File.Open (filePath, FileMode.Open); StreamReader sr = new StreamReader (fs); string txtData = sr.ReadToEnd (); //Cut each line string[] lineData = txtData. Split ('\n'); //Incontainer array initialization rec.sites = new Site[lineData. Length -1]; for (int i = 1; i < lineData.Length; i++) { int index = 0; //Cut Element String string[] siteTemp = lineData [i]. Split ('|'); //Assign value to array in container rec.sites [i - 1] = new Site (){Name =siteTemp[index++],Url = siteTemp[index++] }; } }
SitesList siteList = new SitesList (); TxtToClass (out siteList, txtPath); foreach (Site item in siteList.sites) { Debug.Log (item. Name + "|" + item.Url); }
TXT update
Storage and parsing of JSON files
using LitJson;
JSON saving
//Convert classes to Json objects public void SiteToJson(SitesList siteList,string filePath) { string jsonData = JsonMapper.ToJson (siteList); //It can not be written. The pen method will automatically process according to the parameters if (! File.Exists(filePath)) { //Create a new file if the file does not exist FileStream fstemp = File.Create (filePath); fstemp.Close (); Debug.Log ("File does not exist, new file has been created"); } //Open the file stream. Create mode means to create if it does not exist, and overwrite the original content if it exists FileStream fs = File.Open (filePath, FileMode.Create); //Create StreamWriter StreamWriter sw = new StreamWriter (fs, System.Text.Encoding.UTF8); sw.Write (jsonData); //Empty buffer and ensure writing sw.Flush (); //Close StreamWriter sw.Close (); //Close file stream fs.Close (); Debug.Log ("Write Complete"); }
Site s1=new Site() {Name="Coming and going", Url=“ http://www.azimiao.com "}; Site s2 = new Site (){ Name = "Xema", Url = "https://" }; Site s3 = new Site (){ Name ="Pret",Url = "https://"}; siteList.sites = new Site[]{ s1,s2,s3}; SiteToJson (siteList, jsonPath);
{ "sites": [ {"Name":"\u6893\u55B5\u51FA\u6CA1","Url":" http://www.azimiao.com "}, {"Name":"Xema","Url":"https://"}, {"Name":"Pret","Url":"https://"} ] }
JSON reading and formatting
//Convert Json to class public void ParseXml(out SitesList rec,string filePath) { if (! File.Exists(filePath)) { Debug.LogWarning (filePath+"does not exist"); return; } //FileMode. Open, Open an existing file. If it does not exist, throw an exception FileStream fs = File.Open (filePath,FileMode.Open); StreamReader sr = new StreamReader (fs); string data = sr.ReadToEnd (); rec = JsonMapper.ToObject<SitesList> (data); //Close Stream sr.Close (); //Close Stream fs.Close (); Debug.Log ("Read Complete"); }
SitesList siteList; ParseXml (out siteList, jsonPath); foreach (Site item in siteList.sites) { Debug.Log (item.Name+"|"+item.Url); }
to update
XML
using System.Xml;
<? xml version="1.0" encoding="UTF-8" ?> <Sites></Sites>
XML addition and saving
public void ClassToXml(SitesList sitesList,string filePath) { XmlDocument doc = new XmlDocument (); //Load File doc.Load (filePath); //Get Root Node Sites XmlNode root = doc.SelectSingleNode ("Sites"); foreach (Site item in sitesList.sites) { XmlElement site = doc.CreateElement("Site"); XmlElement name = doc.CreateElement ("Name"); name.InnerText = item.Name; XmlElement url = doc.CreateElement ("Url"); url.InnerText = item.Url; //Add Child Node site.AppendChild (name); site.AppendChild (url); root.AppendChild (site); } //Save File doc.Save (filePath); }
Site s1=new Site() {Name="Coming and going", Url=“ http://www.azimiao.com "}; Site s2 = new Site (){ Name = "Xema", Url = "https://" }; Site s3 = new Site (){ Name ="Pret",Url = "https://"}; SitesList siteList = new SitesList (); siteList.sites = new Site[]{ s1,s2,s3}; ClassToXml(siteList,xmlPath);
XML reading and parsing
public void XmlToClass(out SitesList rec, string filePath) { XmlDocument doc = new XmlDocument (); doc.Load (filePath); XmlNode root = doc.SelectSingleNode ("Sites"); XmlNodeList xmlSites = root.SelectNodes ("Site"); rec = new SitesList (); rec.sites = new Site[xmlSites. Count]; int index = 0; foreach (XmlNode item in xmlSites) { string name = item ["Name"]. InnerText; string url = item ["Url"]. InnerText; Site temp = new Site (){ Name = name, Url = url }; rec.sites [index] = temp; index++; }
SitesList siteList = new SitesList (); XmlToClass (out siteList, xmlPath); foreach (Site item in siteList.sites) { Debug.Log (item. Name + "|" + item.Url); }
XML Update
XML deletes a node
summary
References
-
[Header] [Unity] Unity Japan UnityChanSD Role