Unity XML Read and write
XML is a commonly used data format to facilitate index lookups of data
1. First introduce the relevant dynamic link library:
1.1system.data.dll
1.2excel.dll
1.3 File Application Header
using Unityengine; using System.Collections; using System.IO; using System.Xml; using System.Data; using System.Collections.Generic; using System.Linq; using Excel;
2. Generate the corresponding XML (write XML) based on Excel
2.1:excel to a common dataset format
/// <summary> ///generate a corresponding dataset based on Excel's path/// </summary> /// <param name= "path" ></excel address: often application.datapath+ "/streamingassets/" + "Excel file name. xlsx" > /// <returns></returns> PublicDataSet GetDataSet (stringpath) {FileStream FS=File.Open (Path, FileMode.Open, FileAccess.Read); Iexceldatareader Excelreader=Excelreaderfactory.createopenxmlreader (FS); DataSet DS=Excelreader.asdataset (); Fs. Dispose (); returnds; }
2.2: Generate the corresponding XML based on the table reference custom method in the 2.1 generated dataset
/// <summary> ///XML serialization of DataSet data based on a string index/// </summary> /// <param name= "XMLName" ></generated XML name > /// <param name= "DT" ></dataset table> /// <param name= "Stratt" ></parent node sequence > /// <param name= "strelement" ></sub-node sequence > Public voidToxml (stringXmlname,datatable DT,stringStratt,string[] strelement) { if(DT! =NULL) { if(DT. Rows.Count >0) { if(!directory.exists (Application.streamingassetspath)) {directory.createdirectory (Application.streamingassetspath); } stringPath = Application.streamingassetspath +"/"+ XMLName +". XML"; if(file.exists (path)) {file.delete (path); } XmlDocument writer=NewXmlDocument (); XmlElement x100=writer. CreateElement (Stratt); for(inti =0; I < DT. Rows.Count; i++) {XmlElement x10= writer. CreateElement (strelement[0]); for(intj =0; J < Strelement. length-1; J + +) {XmlAttribute xa=writer. CreateAttribute (Strelement[j]); Xa. Value=DT. ROWS[I][J]. ToString (); X10. Attributes.append (XA); } x100. AppendChild (x10); } writer. AppendChild (x100); Writer. Save (path); } } }
3. Common access to data in XML (read XML)
Public stringGettextnamebygameobjectname (stringTargetparentname,stringgameobjname) {testname=NULL; XmlNodeList XmlNodeList= Doc. selectSingleNode ("Test"). ChildNodes; foreach(XmlElement TempnodeinchXmlNodeList) { if(Targetparentname = = Tempnode. GetAttribute ("Parenttargetname") && Gameobjname = = Tempnode. GetAttribute ("Scenesname") ) {testname= Tempnode. GetAttribute ("Textname"); } Else { //debug.logerror ("table didnt has the Attribute"); } } returntestname; }
5. Writing XML
Public voidWritetoxml (stringNodeattritube,stringInnertex) {_xmlpath= Application.datapath +"/streamingassets/"+"Che.xml"; Loadxmlfilebypath (_xmlpath); XmlNodeList XmlNodeList= Doc. selectSingleNode ("Synthesis Tools List"). ChildNodes; foreach(XmlElement TempnodeinchXmlNodeList) { if(Nodeattritube = = Tempnode.getattribute ("Tool4id") {Tempnode.setattribute ("Tool4id", Innertex); }} doc. Save (_xmlpath); }
4. Add Editor function, make it easy to plot and generate XML after changing table.
add a method to a class Public voidCreatxml () {_excelpath= Application.datapath +"/streamingassets/"+"explosionlist.xlsx"; _xmlpath= Application.datapath +"/streamingassets/"+"Che.xml"; DS=GetDataSet (_excelpath); Toxml ("che"Ds. tables[0],"Synthesis Tools List", NEWSTRESS0); } In another editor class write Public classCompositexmlmake:editor {[MenuItem ("Tools/creatcompositexml")] Public Static voidCreatxml () {conpositexml.getinstance (). Creatxml (); }
Read and write and create unity XML