首页 > 代码库 > unity xml的读取和写入和创建

unity xml的读取和写入和创建

unity xml的读取和写入

Xml是一种常用的数据格式,方便数据的索引查找

 

1.首先引入相关的动态链接库:

1.1System.Data.dll

1.2Excel.DLL

1.3文件应用抬头

using UnityEngine;
using System.Collections;
using System.IO;
using System.Xml;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using Excel;

 

2.根据excel生成对应的xml(写xml)

2.1:excel转为常用的dataset格式

/// <summary>
    /// 根据Excel的path生成对应的dataset
    /// </summary>
    /// <param name="path"></Excel地址:常为Application.dataPath+ "/StreamingAssets/" + "Excel文件名.xlsx">
    /// <returns></returns>
    public DataSet getDataset(string path)
    {
        FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(fs);          
        DataSet ds = excelReader.AsDataSet();
        fs.Dispose();
        return ds;
    }

2.2:根据2.1生成的dataset中的table参考自定义方式生成对应的xml

  /// <summary>
  /// 将dataset数据根据字符串索引进行xml序列化
  /// </summary>
  /// <param name="xmlname"></生成的xml名字>
  /// <param name="DT"></dataset的table>
  /// <param name="strAtt"></父节点序列>
  /// <param name="strelement"></子节点序列>
   
  public   void Toxml(string xmlname,DataTable DT,string strAtt,string[] strelement)
    {
        if (DT != null)
        {
            if (DT.Rows.Count > 0) {
                if (!Directory.Exists(Application.streamingAssetsPath))
                {
                    Directory.CreateDirectory(Application.streamingAssetsPath);
                }
                string path = Application.streamingAssetsPath + "/" + xmlname + ".xml";
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                XmlDocument writer = new XmlDocument();
                XmlElement x100 = writer.CreateElement(strAtt);
                for (int i = 0; i < DT.Rows.Count; i++) {
                    XmlElement x10 = writer.CreateElement(strelement[0]);
                    for (int j = 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.常见获取xml中的数据(读xml)

  public string GetTextNameByGameObjectName(string TargetParentName,string gameobjName)
    {
        testname = null;
      
        XmlNodeList xmlNodeList = doc.SelectSingleNode("test").ChildNodes;

        foreach (XmlElement tempnode in xmlNodeList)
        {
            if (TargetParentName == tempnode.GetAttribute("parentTargetname") && gameobjName == tempnode.GetAttribute("scenesname"))
            {
                testname = tempnode.GetAttribute("textname");

            }
            else
            {
              //  Debug.LogError("table didnt have the Attribute");
            }         
            }
        return testname;   
    }

 5.写入xml

  public void WriteToxml(string NodeAttritube,string InnerTex)
    {
        _XmlPath = Application.dataPath + "/StreamingAssets/" + "che.xml";
        LoadXmlFileByPath(_XmlPath);
        XmlNodeList xmlNodeList = doc.SelectSingleNode("合成工具列表").ChildNodes;
        foreach (XmlElement tempNode in xmlNodeList)
        {

            if (NodeAttritube == tempNode.GetAttribute("Tool4ID"))
            {

                tempNode.SetAttribute("Tool4ID", InnerTex);
            }

          
        }    
        doc.Save(_XmlPath);
          
    }

 

4.增加编辑器功能,方便策划改表后自行生成xml

在类中增加方法 
public void CreatXml()
    {
        _Excelpath= Application.dataPath + "/StreamingAssets/" + "ExplosionList.xlsx";
        _XmlPath = Application.dataPath + "/StreamingAssets/" + "che.xml";
         ds =  getDataset(_Excelpath);
         Toxml("che", ds.Tables[0], "合成工具列表", newstress0);
       
    }


在另外的编辑器类写入
public class CompositeXmlMake : Editor {
    [MenuItem("Tools/CreatCompositeXml")]

    public static void CreatXML()
    {
        ConpositeXml.GetInstance().CreatXml();
    }

 

unity xml的读取和写入和创建