Read and write and create unity XML

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.